Ok so...my demystifying APIs for traditional infrastructure personnel blog series is snowballing into more posts. Gah! Hopefully you're finding them informative and helpful. If you're just landing on this post as an initial glance, please take some time to absorb the following posts:
So, what's next? HTTP requests! During an API (Application Programming Interface) call, the request components in the HTTP (Hypertext Transfer Protocol) section of the call play a crucial role in defining the information being sent to the server and the desired action to be performed. These components are part of the HTTP request sent from a client (such as a web browser or a program) to the server, instructing it on what to do and conveying any necessary data. The main components of an HTTP request are as follows:
Request Line - The request line is the first line of an HTTP request and contains three important pieces of information:
1. HTTP Method (which we covered in this post): It indicates the type of action the
client wants the server to perform. Common methods include:
GET: Used to retrieve data from the server.
POST: Used to submit data to be processed by the server.
PUT: Used to update existing data on the server.
DELETE: Used to remove data from the server.
PATCH: Used to apply partial modifications to a resource.
2. Request URI (Uniform Resource Identifier): It specifies the path to the resource
on the server that the client wants to interact with.
3. HTTP Version: It specifies the version of the HTTP protocol being used (e.g.,
HTTP/1.1 or HTTP/2). For purposes of this blog post, I won't go into much depth
understanding in this post, HTTP/2 offers several performance and efficiency
improvements over HTTP/1.1, making it a preferred choice for modern web
applications and websites. It is essential to ensure that the server and client both
support HTTP/2 to take full advantage of its benefits.
To better illustrate, here's an example of what a Request Line would look like (notice I'm using the 333 employee number example from this post):
GET /api/users/333 HTTP/1.1
Request Headers - HTTP request headers are additional pieces of information sent in the request to provide extra context or instructions to the server. Some common headers include:
Host: Specifies the target server's hostname and port number.
User-Agent: Identifies the client making the request (e.g., the browser or application).
Content-Type: Specifies the format of the data being sent in the request body (for POST or PUT requests).
Authorization: Used to pass credentials or access tokens for authentication purposes.
Accept: Informs the server about the preferred response format (e.g., JSON, XML).
Here's an example of what a request header would look like:
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64)
Authorization: Bearer abcdefghijklmnopqrstuvwxyz
Request Body - The request body is present in POST, PUT, PATCH, and other request methods where the client needs to send data to the server. It carries the payload or information that needs to be processed or stored on the server.
Here's an example of what a request body would look like in JSON format:
"name": "Shannon Kuehn",
"email": "[email protected]",
Query Parameters (optional) - Query parameters are used to pass additional information to the server as part of the URL. They are commonly used with GET requests to filter or modify the response. Typically, a query will show up after a question mark (?) in the URI/URL request. Additionally, you may also see a few parameters passed by using an ampersand (&).
Here's an example of using a query parameter:
In summary, during an API call, the HTTP request components are the request line, headers, request body (if applicable), and query parameters (if applicable). These components define the nature of the request and provide necessary data for the server to process and respond accordingly.