0

To make a network request, I'm using:

session.dataTask(with: urlRequest) { data, response, error in...

I'm trying to specifically handle different cases of getting a 400 response from the server. Meaning, I'm purposely trying to have failing requests, whose responses are 400 errors. error is nil, as expected.

The data passed into the closure is always empty, and response.status.localizedDescription just says "bad request".

On the backend, we're able to see which error was actually thrown, but not on the client.

How can I see the details of the failed request's response? For example, "Bad Request: email is already in use".

Happy to expand or clarify as needed.

ArielSD
  • 829
  • 10
  • 27
  • "error is nil as expected" --> if you're purposefully hitting an error case, `error` should not be nil. `data` should be nil in an error scenario. I would expect to get the details of the error from the `error.localizedDescription`. – johnny Feb 11 '20 at 21:28
  • @wilc0 In this request, `error` is `nil`, and `data` is `0 bytes`? I wouldn't expect a server error, because it's a client error - a bad request. Does that make my issue clearer? – ArielSD Feb 11 '20 at 21:37
  • Any http request with a status code that does not equal `2xx` will result in `data` being nil or 0 bytes and `response` containing the status code and a localizedString of that status code (which is not customizable as far as I can tell). To add any additional details to the error, you will have to add a custom http header that contains some more information: https://developer.apple.com/documentation/foundation/httpurlresponse/1417930-allheaderfields – johnny Feb 11 '20 at 22:02
  • Here are a few related questions that show how you can parse through the various objects that are returned in the completionHandler: https://stackoverflow.com/a/22208113/4608154 and https://stackoverflow.com/a/55205384/4608154 – johnny Feb 11 '20 at 22:04

0 Answers0