For the More Curious: HTTP

When URLSessionTask interacts with a web server, it does so according to the rules outlined in the HTTP specification. The specification is very clear about the exact format of the request/response exchange between the client and the server. An example of a simple HTTP request is shown in Figure 20.6.

Figure 20.6  HTTP request format

HTTP request format

An HTTP request has three parts: a request line, request headers, and an optional request body. The request line is the first line of the request and tells the server what the client is trying to do. In this request, the client is trying to GET the resource at /index.html. (It also specifies the HTTP version that the request will be conforming to.)

The word GET is an HTTP method. While there are a number of supported HTTP methods, you will see GET and POST most often. The default of URLRequest, GET, indicates that the client wants a resource from the server. The resource requested might be an actual file on the web server’s filesystem, or it could be generated dynamically at the moment the request is received. As a client, you should not care about this detail, but more than likely the JSON resources you requested in this chapter were created dynamically.

In addition to getting things from a server, you can send it information. For example, many web servers allow you to upload photos. A client application would pass the image data to the server through an HTTP request. In this situation, you would use the HTTP method POST, and you would include a request body. The body of a request is the payload you are sending to the server – typically JSON, XML, or binary data.

When the request has a body, it must also have the Content-Length header. Handily, URLRequest will compute the size of the body and add this header for you.

Here is an example of how you might POST an image to an imaginary site using a URLRequest.

    if let someURL = URL(string: "http://www.photos.example.com/upload") {
        let image = profileImage()
        let data = image.pngData()

        var req = URLRequest(url: someURL)

        // Adds the HTTP body data and automatically sets the content-length header
        req.httpBody = data

        // Changes the HTTP method in the request line
        req.httpMethod = "POST"

        // If you wanted to set a request header, such as the Accept header
        req.setValue("text/json", forHTTPHeaderField: "Accept")
    }

Figure 20.7 shows what a simple HTTP response might look like. While you will not be modifying the corresponding HTTPURLResponse instance, it is nice to understand what it is modeling.

Figure 20.7  HTTP response format

HTTP response format

As you can see, the format of the response is not too different from the request. It includes a status line, response headers, and, of course, the response body. Yes, this is where that pesky 404 Not Found comes from!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.151.141