[英]URLSession.shared.dataTask Data is Always Empty When Hitting Node Express Endpoint
I have a simple Node Express endpoint that I can successfully hit with curl: 我有一个简单的Node Express端点,可以使用curl成功击中它:
curl --header "Content-Type: application/json" \
--request POST \
--data '{"lat":33.485834000000001,"long":-100.106417}' \
http://192.168.99.100:49160/login
{"status":200,"data":{"userId":"bff37d9c-25a9-421b-aa58-eba0fef0da03"},"message":null}
However, when I try to hit the same endpoint but using Swift 4 with the following: 但是,当我尝试击中相同的端点但在以下情况下使用Swift 4时:
let task = URLSession.shared.dataTask(with: urlRequest) { data, response, error in
guard let serverData = data, error == nil else {
handler(ResponseBuilder().set(error: ErrorType.network).build())
return
}
// Process data.
}
task.resume()
The 'data' that is returned to me by the URLSession has nothing in it. URLSession返回给我的“数据”中没有任何内容。 I expect the data the contain the same value as what curl returned above.
我希望数据包含与上面返回的curl相同的值。
The 'response' is populated as I would expect: 就像我期望的那样填充了“响应”:
Optional<NSURLResponse>
- some : <NSHTTPURLResponse: 0x600000a1a5a0> { URL: http://192.168.99.100:49160/login } { Status Code: 200, Headers {
Connection = (
"keep-alive"
);
"Content-Length" = (
86
);
"Content-Type" = (
"application/json; charset=utf-8"
);
Date = (
"Thu, 25 Oct 2018 05:51:39 GMT"
);
Etag = (
"W/\"56-8ZCdAcNDopMfakzfNDEniZ0fgoc\""
);
"X-Powered-By" = (
Express
);
} }
'error' is nil and I am seeing logging on the server to indicate that it is being hit but data is still empty. “错误”为零,我在服务器上看到登录消息,表明它已被命中,但数据仍然为空。
I am running my iOS app using the Simulator. 我正在使用模拟器运行iOS应用。
Am I missing something obvious here? 我在这里错过明显的东西吗?
UPDATE 1 更新1
To answer some of the questions below. 要回答以下一些问题。 'urlRequest' is created as follows:
“ urlRequest”的创建如下:
var urlRequest = URLRequest(url: request.url)
urlRequest.addValue("application/json", forHTTPHeaderField: "Content-Type")
urlRequest.addValue("application/json", forHTTPHeaderField: "Accept")
urlRequest.httpMethod = "POST"
urlRequest.httpBody = try? JSONSerialization.data(withJSONObject: request.data, options: .prettyPrinted)
where 'request' is just a wrapper object I use and it's contents is: 其中“请求”只是我使用的包装对象,其内容是:
Request
▿ url : http://192.168.99.100:49160/login
- _url : http://192.168.99.100:49160/login
▿ data : 2 elements
▿ 0 : 2 elements
- key : "lat"
- value : 33.485834000000001
▿ 1 : 2 elements
- key : "long"
- value : -100.106417
UPDATE 3 更新3
As someone suggested I installed Charles Proxy. 有人建议我安装了Charles Proxy。 Here are the results:
结果如下:
Request: 请求:
{
"lat" : 33.485834000000001,
"long" : -100.106417
}
Response: 响应:
{"status":200,"data":{"userId":"3d559555-c0ad-4e3a-a10b-3e267ee7f662"},"message":null}
The unfortunate answer is likely that the xcode debugger is unreliable in this situation (completion handler in a URLSession context) - it always shows data as having 0 bytes. 不幸的答案是,在这种情况下,xcode调试器可能不可靠(URLSession上下文中的完成处理程序)-它始终将数据显示为0字节。
Print the data using either: 使用以下任一方法打印数据:
let received = String(data: data!, encoding: String.Encoding.utf8)
print("\(received)")
or 要么
let resultObject = try JSONSerialization.jsonObject(with: data!, options: [.allowFragments,])
print("\(resultObject)")
try not to strain your neck from shaking your head too hard... 尽量不要过分摇头以拉紧脖子...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.