Every RESTful API is built on HTTP. If you understand HTTP well, much of REST will feel natural. So we'll start here.
The Internet and the Web
People often use these two terms for the same thing, but they're different:
- The Internet is the network that connects computers.
- The World Wide Web is one application that runs on the Internet. Email, FTP, and SSH are others.
Tim Berners-Lee proposed the Web at CERN in 1989. It rests on three ideas:
| Idea | What it does |
|---|---|
| URL: Uniform Resource Locator | Names anything on the network with a single string |
| HTML: Hypertext Markup Language | Describes documents that link to other documents |
| HTTP: Hypertext Transfer Protocol | Moves those documents between computers |
APIs usually send data instead of HTML pages, but they use URLs and HTTP in the same way. An API is a website for programs.
A request and a response
HTTP has two sides. A client sends a request, and a server sends back a response. Your browser is a client, and so is curl, a mobile app, or a program calling an API.
Run this request. It asks the Numbers API to add two numbers.
The response body is 7. There's more in the response than that, though. Add -i to see all of it:
The first line is the status: 200 OK means that the request worked. The lines after it are headers. After a blank line comes the body.
Your turn: Change the numbers and run it again. Which parts of the response change? Which stay the same?
Not every response is JSON
An API can return any kind of data. The FIGlet API returns plain text.
The content-type header says what kind of data the body contains. Here, it's text/plain.
Now try a JSON API. This one returns a contact from an address book. We'll use the Contacts API for most of this course.
Compare: What's the
content-typeof this response? How would a program that receives the response know how to read the body?
The same request in a form
The API explorer sends the same kind of request as curl. Select a method, edit the URL, and press Send.
Your turn: Open the URL
https://numbers.apps.pipal.in/add?a=3&b=4in your browser. Then open your browser's developer tools, go to the Network tab, and reload the page. Click the request to see its headers. Your browser speaks HTTP too.
Summary
- The Web is an application on the Internet, built on URLs, HTML, and HTTP.
- In HTTP, a client sends a request and a server sends back a response.
- A response has a status, headers, and a body.
Next, we'll look closely at the URL.