An API request allows you to retrieve and send data to a data source or server. The API request is formed up of four things:
- The endpoint
- The method
- The headers
- The data (or body)
1. Endpoint
Endpoint or route is the URL you request for. It follows this structure:
Syntax
Root Endpoint + Resource + Parameters (Query/Path/Any other)
- Root Endpoint: The root-endpoint is the starting point of the API you’re requesting. The root-endpoint of Twitter APIs is https://api.twitter.com and Facebook graph API is https://graph.facebook.com/.
- Resource: /cars, /fruits, /user, /product
- Parameters: Query, Path, Form or any other type of parameter.
Query parameters consist of key-value pairs which always begin with a question mark (?). Each parameter pair is then separated with an ampersand (&), like this:
?query1=value1&query2=value2
PATH Parameter: required to access the resource E.g. /user/1 (/user/{user_id})
# /product/{product_id}
/product/10
2. HTTP Method
The following are the most popular:
- Get (Search for data)
- Post (Send data)
- Put (Update data)
- Patch (Partial Update)
- Delete (Delete data)
3. Request Headers
The additional details provided for communication between client and server. Some of the common headers are:
- accept-language
- Accept-Charset
- Accept-Encoding
- Authorization
- Content-Type
4. Request Body
Also called request data, request message, body parameters or payload. contains the info you want to send to the server. Like for login request you send information like username and password.
{
"gender": "Male",
"name": "Harry Potter",
"email": "hoggwarts@wiz.com",
"status": "active"
}