简体   繁体   中英

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:

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:

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. I expect the data the contain the same value as what curl returned above.

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.

Am I missing something obvious here?

UPDATE 1

To answer some of the questions below. 'urlRequest' is created as follows:

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

As someone suggested I installed 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.

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM