ios - Media type is unsupported error in json post method Swift -


i'm new swift , making simple application converts celsius fahrenheit using : json webservice

my code on button btn action:

 @ibaction func btn(sender: anyobject) {     let celnum = txtfirld.text      let myurl = nsurl(string: "http://webservices.daehosting.com/services/temperatureconversions.wso");     print("pass 1")     let request = nsmutableurlrequest(url: myurl!);     request.httpmethod = "post";     print("pass 2")     let poststring = "ncelsius=\(celnum)"     request.httpbody = poststring.datausingencoding(nsutf8stringencoding);     let task = nsurlsession.sharedsession().datataskwithrequest(request){         data, response, error in          print("pass 3")          if error != nil {             print("error 1")             return         }         let responsestring = nsstring(data: data!, encoding: nsutf8stringencoding)         print("responsestring =  \(responsestring)")          do{             let json = try nsjsonserialization.jsonobjectwithdata(data!, options: .mutableleaves) as? nsdictionary             if let parsejson = json{                 let resultvalue = parsejson["status"] as! string!                 print("result:\(resultvalue)")               }          } catch {print("error 2")}     }      task.resume()   } 

but giving me error on console:

pass 1 pass 2 pass 3 responsestring =  optional(the server cannot service request because media type unsupported.) error 2 

plaese thank u :)

1 - should set request content-type :

request.setvalue(" application/json; charset=utf-8", forheader:"content-type")

2 - body not in json format, use :

let params = ["ncelscius" : 1212] request.httpbody = nsjsonserialization.datawithjsonobject(params, options: nsjsonwritingoptions()) 

Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo