For the More Curious: The Request Body

When NSURLSessionTask talks to a web server, it uses the HTTP protocol. This protocol says that any data you send or receive must follow the HTTP specification. The actual data transferred to the server in this chapter is shown in Figure 21.7.

Figure 21.7  HTTP request format

HTTP request format

NSURLRequest has a number of methods that allow you to specify a piece of the request and then properly format it for you.

Any service request has three parts: a request-line, the HTTP headers, and the HTTP body, which is optional. The request-line (which Apple calls a status 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 /courses.json. (It also specifies the HTTP specification version that the data is in.)

The command GET is an HTTP method. While there are a number of supported HTTP methods, you most commonly see GET and POST. The default of NSURLRequest, GET, indicates that the client wants something from the server. The thing that it wants is called the Request-URI (/courses.json).

In the early days of the web, the Request-URI would be the path of a file on the server. For example, the request http://​www.website.com/​index.html would return the file index.html, and your browser would render that file in a window. Today, we also use the Request-URI to specify a service that the server implements. For example, in this chapter, you accessed the courses service, supplied parameters to it, and were returned a JSON document. You are still GETting something, but the server is more clever in interpreting what you are asking for.

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 a service request. In this situation, you use the HTTP method POST, which indicates to the server that you are including the optional HTTP body. The body of a request is data you can include with the request – typically XML, JSON, or Base-64 encoded data.

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

N​S​U​R​L​ ​*​s​o​m​e​U​R​L​ ​=​ ​[​N​S​U​R​L​ ​U​R​L​W​i​t​h​S​t​r​i​n​g​:​@​"​h​t​t​p​:​/​/​w​w​w​.​p​h​o​t​o​s​.​c​o​m​/​u​p​l​o​a​d​"​]​;​
U​I​I​m​a​g​e​ ​*​i​m​a​g​e​ ​=​ ​[​s​e​l​f​ ​p​r​o​f​i​l​e​P​i​c​t​u​r​e​]​;​
N​S​D​a​t​a​ ​*​d​a​t​a​ ​=​ ​U​I​I​m​a​g​e​P​N​G​R​e​p​r​e​s​e​n​t​a​t​i​o​n​(​i​m​a​g​e​)​;​

N​S​M​u​t​a​b​l​e​U​R​L​R​e​q​u​e​s​t​ ​*​r​e​q​ ​=​
 ​ ​ ​ ​[​N​S​M​u​t​a​b​l​e​U​R​L​R​e​q​u​e​s​t​ ​r​e​q​u​e​s​t​W​i​t​h​U​R​L​:​s​o​m​e​U​R​L​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​c​a​c​h​e​P​o​l​i​c​y​:​N​S​U​R​L​R​e​q​u​e​s​t​R​e​l​o​a​d​I​g​n​o​r​i​n​g​C​a​c​h​e​D​a​t​a​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​t​i​m​e​o​u​t​I​n​t​e​r​v​a​l​:​9​0​]​;​

/​/​ ​T​h​i​s​ ​a​d​d​s​ ​t​h​e​ ​H​T​T​P​ ​b​o​d​y​ ​d​a​t​a​ ​a​n​d​ ​a​u​t​o​m​a​t​i​c​a​l​l​y​ ​s​e​t​s​ ​t​h​e​ ​C​o​n​t​e​n​t​-​L​e​n​g​t​h​ ​h​e​a​d​e​r​
r​e​q​.​H​T​T​P​B​o​d​y​ ​=​ ​d​a​t​a​;​

/​/​ ​T​h​i​s​ ​c​h​a​n​g​e​s​ ​t​h​e​ ​H​T​T​P​ ​M​e​t​h​o​d​ ​i​n​ ​t​h​e​ ​r​e​q​u​e​s​t​-​l​i​n​e​
r​e​q​.​H​T​T​P​M​e​t​h​o​d​ ​=​ ​@​"​P​O​S​T​"​;​

/​/​ ​I​f​ ​y​o​u​ ​w​a​n​t​e​d​ ​t​o​ ​s​e​t​ ​t​h​e​ ​C​o​n​t​e​n​t​-​L​e​n​g​t​h​ ​p​r​o​g​r​a​m​m​a​t​i​c​a​l​l​y​.​.​.​
[​r​e​q​ ​s​e​t​V​a​l​u​e​:​[​N​S​S​t​r​i​n​g​ ​s​t​r​i​n​g​W​i​t​h​F​o​r​m​a​t​:​@​"​%​d​"​,​ ​d​a​t​a​.​l​e​n​g​t​h​]​
 ​ ​ ​ ​f​o​r​H​T​T​P​H​e​a​d​e​r​F​i​e​l​d​:​@​"​C​o​n​t​e​n​t​-​L​e​n​g​t​h​"​]​;​

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

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