0

Im attemping to make a HTTPRequest using NSURLSession. When I set the full url the request returns the correct data but when using parameters (NSJSONSerialization.dataWithJSONObject -> HTTPBody I get this error

error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost."

is there something im doing wrong here?

 let json = ["api_key": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"]
    do {

        let jsonData = try NSJSONSerialization.dataWithJSONObject(json, options: .PrettyPrinted)

        let url = NSURL(string: "https://api.themoviedb.org/3/discover/movie")!
        let request = NSMutableURLRequest(URL: url)
        request.HTTPBody = jsonData
        request.HTTPMethod = "GET"
        request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")


        let task = NSURLSession.sharedSession().dataTaskWithRequest(request){ data, response, error in
            if error != nil{
                print("Error -> \(error)")
                return
            }
            do {
                let result = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? [String:AnyObject]

                print("Result -> \(result)")

            } catch {
                print("Error -> \(error)")
            }
        }

        task.resume()


    } catch {
        print(error)
    }
}

This is not a duplicate! I looked at the suggested answer (none of them worked) before asking this question

Terrance
  • 275
  • 1
  • 3
  • 11

1 Answers1

0

In your case that issue can be solved by changing the request.HTTPMethod = "GET" to request.HTTPMethod = "POST"

You should not send HTTP Body in the get request, to send the data with the body you should change HTTPMethod to post

Note: Please check if this api method supports POST requests, if it don't support post you can't use it with http body/post, as per doc i only find 'get' request for the discover/movie which can be like this:

let url = NSURL(string: "http://api.themoviedb.org/3/discover/movie?api_key=YOUR_API_KEY")!
let request = NSMutableURLRequest(URL: url)
request.addValue("application/json", forHTTPHeaderField: "Accept")

let session = NSURLSession.sharedSession()
let task = session.dataTaskWithRequest(request) { data, response, error in
    if let response = response, data = data {
        print(response)
        print(String(data: data, encoding: NSUTF8StringEncoding))
    } else {
        print(error)
    }
}

task.resume()

Ref: You can check more information from this url: http://docs.themoviedb.apiary.io/#reference/discover/discovermovie/get

HardikDG
  • 5,892
  • 2
  • 26
  • 55
  • The request get executed now but the parameters are still not recognized – Terrance Apr 08 '16 at 03:45
  • @Terrance updated the answer check the portion with note, also check if this url supports post ? – HardikDG Apr 08 '16 at 04:00
  • That will work but not really effective in my situation. Some requests take multiple parameters. I ended up using this answer http://stackoverflow.com/a/27724627/6091482. Thanks for your help though! – Terrance Apr 08 '16 at 04:12