0

I want to make a HTTP request to a server but I have troubles parsing my data to JSON.

This is my code:

let dic = ["interest":["category":"Viajes","value":"Mexico"],"metadata":["version":"0.1","count":1]]
    do{
        let jsonData = try NSJSONSerialization.dataWithJSONObject(dic, options: NSJSONWritingOptions())


        let url:NSURL = NSURL(string: "http://ip/interests")!
        let session = NSURLSession.sharedSession()

        let request = NSMutableURLRequest(URL: url)
        request.HTTPMethod = "POST"
        request.cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringCacheData

        //let paramString = ""
        //request.HTTPBody = paramString.dataUsingEncoding(NSUTF8StringEncoding)
        request.HTTPBody = jsonData
        let dataString = NSString(data: jsonData, encoding: NSUTF8StringEncoding)
        print(dataString)


        let task = session.dataTaskWithRequest(request) {
            (
            let data, let response, let error) in

            guard let _:NSData = data, let _:NSURLResponse = response  where error == nil else {
                print(error)
                return
            }
            print(response?.description)


        }

        task.resume()
    }catch let error as NSError {
        print(error)
        return
    }

The server catch :

{ '{"interest":{"category":"Viajes","value":"Mexico"},"metadata":{"count":1,"version":"0.1"}}': '' }

What I want:

{"interest":{"category":"Viajes","value":"Mexico"},"metadata":{"count":1,"version":"0.1"}}

Anybody knows how to fix it?

Vadim F.
  • 881
  • 9
  • 21
  • 1
    Your error is on server side. – Eric Aya Jul 12 '16 at 05:36
  • look at this post [http://stackoverflow.com/questions/39657745/bind-json-with-post-request/39659453#39659453](http://stackoverflow.com/questions/39657745/bind-json-with-post-request/39659453#39659453) it may help with what you need – Arashk Sep 24 '16 at 17:50

0 Answers0