Chapter 2

2.2. HTTP Response

We have seen what an HTTP Request looks like. Here is what the response looks like:

HTTP/1.1 200 OK
Server: nginx
Date: Fri, 02 Oct 2015 11:54:02 GMT
Content-Type: application/json; charset=utf-8
Connection: close
Vary: Accept-Encoding
P3P: CP="Tumblr's privacy policy is available here:
	https://www.tumblr.com/policy/en/privacy"

{"meta":{"status":200,"msg":"OK"},"response":{"blog":{"title":"","name":"good","posts":2455,"url":"http:\/\/good.tumblr.com\/","updated":1439923053,"description":"*removed for brevity*","is_nsfw":false,"ask":true,"ask_page_title":"Ask GOOD a question","ask_anon":false,"uuid":
"good.tumblr.com","share_likes":true,"likes":430}}}

The first line, is called the status line. It contains 3 parts just like the request line. It contains the protocol version (HTTP/1.1), the response status code (200) and the reason phrase (OK).

Status Codes

The status code is a 3-digit integer that tells us the status of the request. The reason phrase gives a short description of the status code.

Status codes can be categorized into 5 based on the first digit.

1xx - There is additional information about this request.
2xx - The request is successful.
3xx - A redirection will take place to fulfill this request.
4xx - There has been an error from the client.
5xx - There has been an error from the Server.

Common status codes1 and reason phrases you will encounter are:

  • 200 OK - The request is OK.
  • 301 Moved Permanently - The resource you are looking for has been permanently moved.
  • 302 Moved Temporarily - The resource has been temporarily moved.
  • 400 Bad Request - There is an error with your request.
  • 401 Unauthorized - You are not authorized to make this request.
  • 403 Forbidden - You are forbidden to make this request.
  • 404 Not Found - The resource you are looking for cannot be found.
  • 500 Internal Server Error - There has been a server error while responding to your request.

Response Headers

Like request headers, response headers allow the server send additional information about the response or server. Here are the headers in our example response:

Server: nginx
Date: Fri, 02 Oct 2015 11:54:02 GMT
Content-Type: application/json; charset=utf-8
Connection: close
Vary: Accept-Encoding
P3P: CP="Tumblr's privacy policy is available here:
	https://www.tumblr.com/policy/en/privacy"

These headers, well most, are self-explanatory:

  • Server tells us the name of the server.
  • Date is the response’s date and time.
  • Content-Type tells us the response type - application/json.
  • Connection specifies the connection control options. A value of close means the connection will be closed after completion of the response.
  • Vary indicates various versions of the representation exist and tells us what headers can be used to resolve which version to serve. Accept-Encoding signifies a different version (compressed or uncompressed) can be served based on the request Accept-Encoding header. What this means is that if for example we want a compressed version of the response, we can send a request with the header Accept-Encoding: gzip
  • P3P is used to state how information collected about web browser users will be used.

Response Body

The last line is the body itself. Most times, this is our point of interest. Depending on the API, the response body can be returned in different formats that can easily be interpreted.

{"meta":{"status":200,"msg":"OK"},"response":{"blog":{"title":"","name":"good","posts":2455,"url":"http:\/\/good.tumblr.com\/","updated":1439923053,"description":"*removed for brevity*","is_nsfw":false,"ask":true,"ask_page_title":"Ask GOOD a question","ask_anon":false,"uuid":
"good.tumblr.com","share_likes":true,"likes":430}}}

In this example, the body is returned as JSON (JavaScript Object Notation). You will be seeing a lot of JSON in responses and even requests.

JSON is a light-weight format for data exchange. It is built on two data structures:

  • Objects - a collection of name-value pairs.
  • Arrays - an ordered list of values.

These data structures exist across modern programming languages. It therefore makes JSON easy to parse in any modern programming language.

JSON objects have the format:

{"name": value, "another_name": value}

JSON arrays have the format:

[value, value]

value can be a string, object, array, number, boolean (true or false) or null. An object can therefore contain a string, number, an array or another object. An array can as well contain strings, numbers, boolean, null or another array or object.

Here is the JSON response from our request, formatted for clarity. It should make perfect sense now:

{
  "meta": {
    "status": 200,
    "msg": "OK"
  },
  "response": {
    "blog": {
      "name": "good",
      "posts": 2455,
      "ask": true,
      "uuid": "good.tumblr.com",
      "likes": 430
    }
  }
}

The wrapper object contains two objects: meta and response. meta has the name-value pairs status and msg. response has a blog object. The blog object has the name-value pairs name, posts, ask, uuid and likes.

As we move on, we will look at other response formats.

  1. A complete list of HTTP status codes is available here: https://en.wikipedia.org/wiki/List_of_HTTP_status_codes