When your computer connects to the internet, it is given an IP address. This address is a unique identifier for your device on the internet. Every device that joins the internet is assigned an IP address.
There are lots of different types of devices that are connected to the internet. Some of those devices are referred to as servers. The sole purpose of servers is to be on the internet to receive requests from other devices and provide responses to those requests, sending back a resource if it was requested. When you go to a website, you are making a request to a server that interprets the request and sends your browser a response with the requested resource.
There are many ways to make requests, but the most typical is by using your browser. Requests can also be made using a command line utility called
cURL or through other programs and libraries created specifically for making requests – any technique for making requests is called the client.
Most requests that users make on the internet use the HTTP scheme. HTTP stands for hypertext transfer protocol and is the main scheme used by the world wide web for sending requests and responses. (You may also be familiar with FTP (file transfer protocol).) HTTP defines a set of common types of requests and responses that can be interpreted by all devices. Requests made over HTTP are prepended by
HTTP requests, like websites, contain a
head and a
head contains meta information about the request, and the
body contains a resource or resources intended to accompany the request. Whether or not a
body is sent depends on the type of request. HTTP servers are programmed to respond to different types of requests.
head of the request is required and includes a couple important pieces of information: the request method and a resource (often in the form of a URL). There are several other headers that are included with a request, but they are for the most part included by the client.
Common request methods include:
GET: this is the most common request when accessing a website with your browser. A
GETrequest method indicates that the client is requesting some resource from the server, but is not sending any resources along with the request.
POST: sends a request along with a resource to be posted to the server.
bodyof the request.
DELETE: sends a request to delete a resource on the server.
PUT: this method sends a request to post something to the server; if there is already something at the specified location, it is modified.
bodyof the request.
When you type a URL into the browser and press enter, your browser makes a
GET request to a server for a specific resource.
When you make a request, you are requesting some specific resource – whether you want to retrieve something from that location, delete something from that location, or post something to that location, the resource identifies where on the server you’re making the request to.
Most of the time, the resource you’re requesting will be in the form of a URL. URL stands for uniform resource locator and is a human-readable way to identify resources hosted on a server. The URL is broken into a few parts:
index.html), an image (like
puppies.jpg), or some other resource.
PUT request, you can send a resource along with the request in the request body. Usually this is for adding a resource to the database on the server, like the example above of adding a Facebook status update.
A server is a computer that is connected to the internet (so it has an IP address) that has one or more ports that are listening for HTTP requests. The server has code on it that is programmed to respond differently to different types of requests depending on the type of request, the URL of the request, and the request body.
Once the server receives a request, it has to interpret the request and provide the appropriate response. Responses include a status code and sometimes a resource or resources (depending on the request).
Every HTTP request receives a response, whether it’s successful or not. HTTP status codes are pre-defined responses that dictate what is happening with the request and/or how the request went.
There are five groups of HTTP status codes:
1xx: informational; request is still going through, just wanna tell you what’s going on
2xx: success; the request went through without a hitch
3xx: redirection; the request needs to be redirected, but nothing has gone wrong
4xx: client error; the request cannot be fulfilled because of user/client error
5xx: server error; the request looks fine, but something’s going on on the server side and the request can’t be fulfilled
Status codes can provide a lot of information about what’s going on with your code and application. For a full list of HTTP status codes with cute pictures of puppies, see HTTP status dogs. (For the cat fans, there is also HTTP status cats.)
Common HTTP status codes include:
200 OK: Success! Everything worked out just fine.
404 Not Found: there is nothing at the location of the request (you probably typed the URL wrong).
503 Service Unavailable: the server barfed; this is common when a website is receiving lots and lots of traffic and goes down.
Some successful requests result in resources being sent back to the client. This is not true of every request, but many requests yield responses with resources. Responses, just like requests, have a
head and a
body. The status code of the response is included in the response
head, and the resource (if any) is included in the response
If you type a URL into a browser window and press enter, you are making a
GET request for whatever resource is at the other end of that URL. Most of the time this is a HTML webpage. In this case, the server interprets the request, and returns the HTML page to be displayed in your browser.
body of the HTML page. As an example, to load
API stands for application program interface. Web APIs are a pre-defined list of URLs that are used to access specific resources on a server. APIs are often used to provide and manage access to resources located in a database, or used as a way to do processing tasks on a server instead of within an application or in the browser.
Mapbox APIs that provide and manage access to database resources:
Mapbox APIs that do processing tasks:
Mapbox APIs that do both:
HTTP requests can have various types of authentication associated with them natively (built into the request process), and some servers also require separate types of authentication. In the case of Mapbox, we request authentication when accessing our APIs in the form of an access token which is appended to the request as a query parameter.
Authentication (in any form) is a way to track usage and prevent abuse. By authenticating, the user is providing their identity to the HTTP server, which allows the server to keep track of requests and responses per user to make sure that they are only accessing resources to which they should have access, to charge them for specific types of usage, or to collect statistical information.
APIs are commonly used inside of web applications – a resource is requested via an API and the application parses the response and plugs it into the appropriate place in the application. Mapbox Studio is a great example – it is a web application that interacts with Mapbox APIs. When a user goes to the Styles page, Mapbox Studio makes a request to the Styles API to retrieve a list of styles for the user. When Mapbox Studio receives the list in the response (returned as a JSON object), the code in Mapbox Studio iterates through that list, parses each style, and makes an entry for it on the Styles page.