API Request-Response Cycle — How Data Flows Between Applications
In this tutorial, you will learn about API Request. We cover key concepts, practical examples, and best practices to help you master this topic.
Every API interaction follows the request-response cycle: a client constructs an HTTP request with method, URL, headers, and optional body, then the server processes it and returns a response with a status code and payload.
What You'll Learn
- The anatomy of HTTP requests and responses
- How to read and interpret API responses
- Common patterns for structuring API requests
Why It Matters
Every line of code you write to call an API depends on understanding this cycle. Misunderstanding it leads to bugs, security holes, and performance issues.
sequenceDiagram
Client->>Server: GET /api/v1/users HTTP/1.1
Client->>Server: Host: api.example.com
Client->>Server: Authorization: Bearer token123
Server-->>Client: HTTP/1.1 200 OK
Server-->>Client: Content-Type: application/json
Server-->>Client: {"users": [{"id": 1, "name": "Alice"}]}
Code Examples
import requests
# Full request construction
url = "https://api.example.com/v1/users"
headers = {
"Authorization": "Bearer token123",
"Accept": "application/json",
"User-Agent": "MyApp/1.0"
}
params = {"page": 1, "limit": 10}
response = requests.get(url, headers=headers, params=params)
print(f"Status: {response.status_code}")
print(f"Headers: {dict(response.headers)}")
print(f"Body: {response.json()}")
# Full curl request with headers
curl -X GET "https://api.example.com/v1/users?page=1&limit=10" \
-H "Authorization: Bearer token123" \
-H "Accept: application/json" \
-v
// Fetch API with full request/response
fetch('https://api.example.com/v1/users?page=1', {
method: 'GET',
headers: {
'Authorization': 'Bearer token123',
'Content-Type': 'application/json'
}
})
.then(res => {
console.log('Status:', res.status);
console.log('Headers:', res.headers);
return res.json();
})
.then(data => console.log('Body:', data));
Common Mistakes
1. Not Including Required Headers
APIs often require specific headers like Accept or Authorization.
2. Ignoring the Response Status Code
Parsing the body without checking the status code leads to misleading errors.
3. Sending Wrong Content-Type
Sending JSON but declaring Content-Type: text/plain causes server errors.
4. Forgetting URL Encoding
Special characters in query parameters must be URL-encoded.
5. Misunderstanding 3xx Redirects
APIs sometimes redirect. Not following redirects silently loses data.
Practice Questions
- What is the difference between request headers and response headers?
- What does status code 201 mean in a response?
- How does a client know the format of the response data?
- What happens if the server receives a malformed request?
- Why is URL encoding important for query parameters?
Answers:
- Request headers are sent by the client; response headers are returned by the server.
- 201 Created — the server successfully created a new resource.
- From the
Content-Typeheader in the response. - The server returns a 400 Bad Request status code with an error message.
- Special characters like spaces or
&can break the URL structure.
Challenge: Write a script that sends a POST request to create a resource, then reads the Location header from the response to fetch the created resource.
FAQ
What's Next
Compare how API vs Webpage requests differ, then explore API Protocols like HTTP, gRPC, and Graphql.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro