Skip to content

API Request-Response Cycle — How Data Flows Between Applications

DodaTech Updated 2026-06-28 2 min read

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

  1. What is the difference between request headers and response headers?
  2. What does status code 201 mean in a response?
  3. How does a client know the format of the response data?
  4. What happens if the server receives a malformed request?
  5. Why is URL encoding important for query parameters?

Answers:

  1. Request headers are sent by the client; response headers are returned by the server.
  2. 201 Created — the server successfully created a new resource.
  3. From the Content-Type header in the response.
  4. The server returns a 400 Bad Request status code with an error message.
  5. 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 is a safe HTTP method?

: A safe method (GET, HEAD, OPTIONS) does not change server state.

What is an idempotent method?

: An idempotent method (GET, PUT, DELETE) produces the same result no matter how many times you call it.

Can a response have no body?

: Yes. Status codes like 204 No Content and 304 Not Modified return headers only.

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