ios - Swift POST request sends an empty body -


here's code:

func makepostcall(endpoint: string, languagename: string) {     guard let url = url(string: endpoint) else {         print("could not create url.")         return     }      let requestlang: [string: any] = ["name": languagename]     let requestbody = try? jsonserialization.data(withjsonobject: requestlang)      var urlrequest = urlrequest(url: url)     urlrequest.httpbody = requestbody     urlrequest.httpmethod = "post"     let session = urlsession.shared     let task = session.datatask(with: urlrequest) {          data, response, error in         guard let data = data, error == nil else {             print(error?.localizeddescription ?? "no data")             return         }          let responsejson = try? jsonserialization.jsonobject(with: data, options: [])         if let responsejson = responsejson as? [string: any] {             print(responsejson)         }      }     task.resume() } 

this sends {"name": "go"} json dictionary flask. flask supposed append language name array , return full array in response. now, works when send request manually, it's not flask's error. when send above ios, request.json == none in flask console. clearly, i'm sending empty body, shouldn't be. idea went wrong?

i call function as

@ibaction func pressedmakepostcall(_ sender: uibutton) {      makepostcall(endpoint: "http://127.0.0.1:5000/lang", languagename: "go")  } 

i tried adding trailing slash, 404 in console. question similar mine i've found this: how make http post request json body in swift , code identical.

you might want manually:

urlrequest.httpbody = "name=\(languagename)".data(using: .utf8)

use jsonserialization make post body {"name":"abc"} might not supported server


Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -