Search code examples
swifthttpgetrequestalamofire

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, "http://192.168.2.16/APIPruebas/validalogin/{\"usuario\":\"user\",\"pwd\":\"pwd\",\"id\":\"10\",\"ulogin\":\"1\",\"clogin\":\"1\"}")

print(request)

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

http://192.168.2.16/APIPruebas/validalogin/{"usuario":"user","pwd":"pwd","id":"10","ulogin":"1","clogin":"1"}

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

Thanks


Solution

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

    let str = "http://192.168.2.16/APIPruebas/validalogin/{\"usuario\":\"user\",\"pwd\":\"pwd\",\"id\":\"10\",\"ulogin\":\"1\",\"clogin\":\"1\"}"
    
    let encodedUrl = str.stringByAddingPercentEncodingWithAllowedCharacters(.URLQueryAllowedCharacterSet())
    

    Now pass encodedUrl in Alamofire.request method.