Understand HTTP server-side errors and how to debug them.
Apple’s HTTP APIs report transport errors and server-side errors:
A transport error is caused by a problem getting your request to, or getting the response from, the server. These are represented by an
NSErrorvalue, typically passed to your completion handler block or to a delegate method like
url. If you get a transport error, investigate what’s happening with your network traffic. To get started, read Choosing a Network Debugging Tool.
Session(_: task: did Complete With Error:)
The status codes returned by the server (see Section 6 Response Status Codes of RFC 7231) aren’t always easy to interpret. Many HTTP server-side errors don’t give you a way to determine, from the client side, what went wrong. These include the 5xx errors (like 500 Internal Server Error) and many 4xx errors (for example, with 400 Bad Request, it’s hard to know exactly why the server considers the request bad).
The following sections explain how to debug these server-side problems.
Print the HTTP Response Body
In some cases the error response from the server includes an HTTP response body that explains what the problem is. Look at the HTTP response body to see whether such an explanation is present. If it is, that’s the easiest way to figure out what went wrong. For example, consider this standard
URLSession request code.
A server-side error runs the line labeled handle HTTP server-side error. To see if the server’s response contains any helpful hints as to what went wrong, add some code that prints the HTTP response body.
Compare Against a Working Client
If the HTTP response body doesn’t help, compare your request to a request issued by a working client. For example, the server might not fail if you send it the same request from:
A web browser, like Safari
A command-line tool, like
An app running on a different platform
If you have a working client, it’s relatively straightforward to debug your problem:
Use the same network debugging tool to record the requests made by your client and the working client. If you’re using HTTP (not HTTPS), use a low-level packet trace tool to record these requests (see Recording a Packet Trace). If you’re using HTTPS, with Transport Layer Security (TLS), you can’t see the HTTP request. In that case, if your server has a debugging mode that lets you see the plaintext request, look there. If not, a debugging HTTP proxy may let you see the request; see Debugging HTTP Proxies for more information.
Compare the two requests. Focus on the most significant values first. Do the URL paths match? Do the HTTP methods match? Do the
Content-Typeheaders match? What about the remaining headers? Do the request bodies match? If these all match and things still don’t work, you may need to look at more obscure values, like the HTTP transfer encoding and, if you’re using HTTPS, various TLS parameters.
Address any discrepancies.
Retry with your updated client.
If things still fail, go back to step 1.
Debug on the Server
If you don’t have access to a working client, or you can’t get things to work using the steps described in the previous section, your only remaining option is to debug the problem on the server. Ideally, the server will have documented debugging options that offer more insight into the failure. If not, escalate the problem through the support channel associated with your server software.