我在swift 2中的http get请求中有错误

时间:2016-07-16 11:19:30

标签: swift http get request alamofire

我有错误:

  

致命错误:在解包可选值时意外发现nil

使用此Swift代码:

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)

我认为问题是{}

但我不知道我是否正确地使用\字符逃避。

邮递员可以正常使用此请求

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

执行HTTP GET请求的最佳方法是什么?

由于

1 个答案:

答案 0 :(得分:0)

我认为你需要在使用api之前对url进行编码,就像这样

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

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

现在在Alamofire.request方法中传递encodedUrl。