Diego Diego - 1 year ago 84
Swift Question

I have an error in http get request in swift 2

I have the error:

fatal error: unexpectedly found nil while unwrapping an Optional value

With this Swift Code:

import Alamofire
let request = Alamofire.request(.GET, "{\"usuario\":\"user\",\"pwd\":\"pwd\",\"id\":\"10\",\"ulogin\":\"1\",\"clogin\":\"1\"}")


I think that the problem are the { }

But I donĀ“t know if I escape " correctly with the \ character.

In postman works fine with this request{"usuario":"user","pwd":"pwd","id":"10","ulogin":"1","clogin":"1"}

Which is the best metod to do a HTTP GET Request?


Answer Source

I think you need to encode the url before using it with api like this

let str = "{\"usuario\":\"user\",\"pwd\":\"pwd\",\"id\":\"10\",\"ulogin\":\"1\",\"clogin\":\"1\"}"

let encodedUrl = str.stringByAddingPercentEncodingWithAllowedCharacters(.URLQueryAllowedCharacterSet())

Now pass encodedUrl in Alamofire.request method.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download