×We're offering Women's Scholarships for applicants to our February class! Details here.
By the end of this chapter, you should be able to:
curlto make HTTP requests and include headers and data
Before we talk about making HTTP requests, let's review once again what HTTP is. HTTP is a protocol (means of communication) between servers and browsers. HTTP is the underlying protocol used by the web and it defines how information is transmitted. It also specifies how servers and browsers should interact. In HTTP, we have requests that are issued by clients (and sometimes servers) to other servers. Those servers then issue a response back to the originator of the request.
When we issue an HTTP request, we specify the URL that we are making a request to and we can pass additional meta-information to the request by using
headers. You can learn more about headers here. Request headers can include information about:
The same way that we send HTTP headers in a request, we also receive HTTP headers in a response. Reponse headers can include information about:
Now let's understand the HTTP request and response in more detail. To make sample HTTP requests, we will use the
curl command line tool which allows us to issue HTTP requests from the terminal. The curl command accepts quite a few flags including:
-v - for additional information
-H - for adding request headers
-I - including response
-X - for picking an HTTP verb
-d - for passing data in the request
You can learn more about
curl and useful flags right here. There are more friendly tools to make HTTP requests like Postman and HTTPie, but we will start with the basics with
curl as it is a valuable tool to teach the fundamentals.
Let's make a request to
curl and add some request headers:
curl -v 'https://www.rithmschool.com/' \ -H 'accept-encoding: gzip, deflate, sdch, br' \ -H 'accept-language: en-US,en;q=0.8' \ -H 'upgrade-insecure-requests: 1' \ -H 'user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36' \ -H 'accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8' \ -H 'authority: www.rithmschool.com' --compressed
You can copy this command and paste it anywhere in the terminal and press enter. You should see the following information:
First - we see the request headers:
GET / HTTP/1.1 Host: www.rithmschool.com accept-encoding: gzip, deflate, sdch, br accept-language: en-US,en;q=0.8 upgrade-insecure-requests: 1 user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36 accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 authority: www.rithmschool.com
We then see the response headers and the full response:
Notice the format of the request and the response. The HTTP request has the following format:
Request-Line 1 or more headers CRLF # new line [ message-body ]
and the response has the following format:
Status-Line 1 or more headers CRLF # new line [ message-body ]
We call this entire process (an HTTP request being made and an HTTP response being issued), the
Another very important part of HTTP is that when we make a request, we specify the type of HTTP
verb that we want to use. There are quite a few HTTP verbs, but here we'll cover only the most common ones. Different verbs serve different purposes, and only some of them are
idempotent. (Remember what this word means? An idempotent operation is one that will repeatedly produce the same result.)
There are other verbs allowed in the HTTP spec but we do not use them often as web developers. The other verbs are listed for reference:
When a response is issued, a status code is sent to provide more information about the response. The HTTP status codes tell the client the result of making a HTTP request to the server. The general category of status codes is as follows:
As you research more on APIs you will very commonly hear the term
REST API when describing certain kinds of APIs. So what is it?
REST (REpresentational State Transfer) is an architectural style, and an approach to communications that is often used in the development of Web services. The purpose of REST is to provide a framework for creating fast, scalable and reliable APIs. In order for an API to be
RESTful there are some constraints that it must adhere to. You can read more about REST here and here.
When you're ready, move on to How the Web Works Exercises