我正在使用swift编写应用程序并尝试集成Uber api。 我想在终端上做这个非常简单的(!?)请求:
curl -X GET -G 'https://sandbox-api.uber.com/v1/products' -d server_token=*************************************** -d latitude=21.3088619 -d longitude=-157.8086674
为此,我尝试了以下代码:
func performPostUberRequest() {
let url: NSURL = NSURL(string: "https://sandbox-api.uber.com/v1/products")
print("performPostUberRequest Ignited")
let params:[String: AnyObject] = ["server_token" : "***************************************", "latitude" : "21.3088621", "longitude" : "-157.8086632"]
let request: NSMutableURLRequest = NSMutableURLRequest(URL: url)
request.HTTPMethod = "GET"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
do {
try request.HTTPBody = NSJSONSerialization.dataWithJSONObject(params, options: NSJSONWritingOptions())
} catch {
}
print("performPostUberRequest ongoing")
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()){
response, data, error in
if let error = error {
print("error: ")
print(error)
print("data: ")
print(data)
print("response: ")
print(response)
} else if data != nil {
do {
let json: NSDictionary = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions()) as! NSDictionary
print("here comes JASON: ")
print(json)
} catch {
print("Epic Fail")
}
}
}
}
但无论我尝试3-4天,现在给出了完全相同的答案:
performPostUberRequest Ignited
performPostUberRequest ongoing
error:
Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost." UserInfo={NSErrorFailingURLStringKey=https://sandbox-api.uber.com/v1/products, _kCFStreamErrorCodeKey=-4, NSErrorFailingURLKey=https://sandbox-api.uber.com/v1/products, NSLocalizedDescription=The network connection was lost., _kCFStreamErrorDomainKey=4, NSUnderlyingError=0x16da79f0 {Error Domain=kCFErrorDomainCFNetwork Code=-1005 "The network connection was lost." UserInfo={_kCFStreamErrorCodeKey=-4, NSErrorFailingURLStringKey=https://sandbox-api.uber.com/v1/products, NSErrorFailingURLKey=https://sandbox-api.uber.com/v1/products, NSLocalizedDescription=The network connection was lost., _kCFStreamErrorDomainKey=4}}}
data:
nil
response:
nil
修改
我下载了Charles,它给了我以下屏幕:
困扰我的是"SSL Proxying not enabled for this host: enable in Proxy Settings, SSL locations"
这句话。此外,当通过终端输入curl命令时,我无法在Charles上触发任何内容。我想这很正常。
SO上的每个解决方案都告诉我重新启动模拟器,我做了。我进入了模拟器设置并启用了HTTP服务。我吹了墨盒,然后到了谷歌的第2页。请。救命。我
编辑2:解决方案
这是最终的代码,感谢@faarwa
func performPostUberRequest() {
var components = NSURLComponents()
components.scheme = "https"
components.host = "sandbox-api.uber.com"
components.path = "/v1/products"
components.queryItems = [
NSURLQueryItem(name: "server_token", value: "********"),
NSURLQueryItem(name: "latitude", value: "21.3088621"),
NSURLQueryItem(name: "longitude", value: "-157.8086632")
]
let request: NSMutableURLRequest = NSMutableURLRequest(URL: components.URL!)
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()){
print("thanks faarwa")
}
答案 0 :(得分:2)
通过URL发送查询参数,而不是HTTP消息正文,因为它是GET请求。除去:
do {
try request.HTTPBody = NSJSONSerialization.dataWithJSONObject(params, options: NSJSONWritingOptions())
} catch {
}
并将参数添加到NSURL
(例如使用NSURLComponents
)可以解决此问题。