ios - statusCode should be 200, but is 415 -
i trying post simple string, continuously getting http 415 unsupported media type error. tried converting parameter json still didn't work.
method updated
func requestusingpostmethod(url: string, parameter: string, completion: @escaping (_ success: [string : anyobject]) -> void) { //@escaping...if closure passed argument function , invoked after function returns, closure @escaping. var request = urlrequest(url: url(string: url)!) request.addvalue("application/json", forhttpheaderfield: "content-type") request.addvalue("application/json", forhttpheaderfield: "accept") request.httpmethod = "post" let poststring = parameter request.httpbody = try? jsonserialization.data(withjsonobject: [poststring]) // request.httpbody = poststring.data(using: .utf8) let task = urlsession.shared.datatask(with: request) { data, response, error in guard let data = data, error == nil else { // check fundamental networking error print("error=\(string(describing: error))") return } if let httpstatus = response as? httpurlresponse, httpstatus.statuscode != 200 { // check http errors print("statuscode should 200, \(httpstatus.statuscode)") print(response!) return } let responsestring = try! jsonserialization.jsonobject(with: data, options: .allowfragments) as! [string : anyobject] completion(responsestring) } task.resume() }
request
networkcall().requestusingpostmethod(url: "http://192.168.50.119:8181/rest/items/wemo_lamp_switch", parameter: "on", completion: { response in print("--------------------------------------------------------------") print(response) // let jsonresults = json(string: response) })
error
statuscode should 200, 415 { url: http://192.168.50.119:8181/rest/items/wemo_lamp_switch } { status code: 415, headers { "content-length" = 282; "content-type" = "application/json"; date = "tue, 12 sep 2017 08:33:16 gmt"; server = "jetty(9.2.19.v20160908)"; } }
i updated question answers still getting same error.
postman data
{ "id": "6f5d4f8a-612a-10f9-71b5-6dc8ba668885", "name": "simpledata", "description": "", "order": [ "65df8736-1069-b0f0-3a1d-c318ce1810e0" ], "folders": [], "folders_order": [], "timestamp": 1505208950131, "owner": 0, "public": false, "requests": [ { "id": "65df8736-1069-b0f0-3a1d-c318ce1810e0", "headers": "", "headerdata": [], "url": "http://192.168.50.119:8181/rest/items/wemo_lamp_switch", "queryparams": [], "pathvariables": {}, "pathvariabledata": [], "prerequestscript": null, "method": "post", "collectionid": "6f5d4f8a-612a-10f9-71b5-6dc8ba668885", "data": [], "datamode": "raw", "name": "http://192.168.50.119:8181/rest/items/wemo_lamp_switch", "description": "", "descriptionformat": "html", "time": 1505208951279, "version": 2, "responses": [], "tests": null, "currenthelper": "normal", "helperattributes": {}, "rawmodedata": "off" } ] }
request.addvalue("application/json", forhttpheaderfield: "content-type") request.addvalue("application/json", forhttpheaderfield: "accept")
also
let theparam = jsonserialization.data(withjsonobject: parameter)
Comments
Post a Comment