Codecademy Logo

APIs and HTTP Requests

HTTP Requests and Responses

In web development, HTTP (Hypertext Transfer Protocol) is the protocol used to structure requests and responses over the internet. A client, such as a browser, requests resources such as HTML files, images, and scripts from a server. The server sends the requested resources back in a response.

HTTP DNS and TCP

In web development, before an HTTP request is sent, the browser uses the Domain Name System (DNS) to resolve the URL’s domain name to an IP address. The browser then opens a TCP (Transmission Control Protocol) connection to the server. TCP manages the connection, and HTTP defines the commands both sides follow to communicate.

HTTP Message Structure

In HTTP, requests and responses are messages that begin with a single line followed by headers. A request line contains the method, such as GET, the path of the requested resource, and the protocol version. A response’s status line contains the protocol version and a status code, such as 200 OK for a successful request or 404 Not Found for a missing resource. The example shows a request and its response.

GET /index.html HTTP/1.1
Host: www.example.com
HTTP/1.1 200 OK
Content-Type: text/html

HTTPS Encryption

In web development, HTTPS (HTTP Secure) encrypts the data sent between a client and a server, so HTTPS should be used for sensitive information such as passwords and payment details. To support HTTPS, a server needs a certificate issued by a Certificate Authority (CA).

Customizing API Requests

API requests often include additional information to customize the response.

For example, a request to the OpenWeather API should include the location of the requested weather data.

Application Programming Interface

An Application Programming Interface, or API, is a tool that makes it easier for developers to access software from another application.

Browser APIs

Browser APIs, a type of Web API, are provided by browsers to give developers access to information that the browser can access from users’ computers.

For example, the Web Audio API is a browser API that provides a powerful and versatile system for controlling audio on the Web.

Third-Party APIs

Third-party APIs are apps that provide some type of functionality or information from a third-party, usually a company.

For example, the OpenWeather API is a third-party API that provides in-depth weather information.

API Documentation

All third-party and Web APIs provide documentation for how to use the API.

API Keys

Some third-party APIs require an API key which is a special token that is given to a developer to interact with the API. These API keys are unique and should be kept secret.

API Response Data

When a successful request is made to an API, data is sent back. Most APIs return data in the form of JSON, and it is up to developers to decide how to consume it.