Bird
Raised Fist0
Rest APIprogramming~10 mins

Example requests and responses in Rest API - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Example requests and responses
Client prepares request
↓
Send HTTP request to server
↓
Server receives request
↓
Server processes request
↓
Server sends HTTP response
↓
Client receives response
↓
Client processes response
This flow shows how a client sends a request to a server and receives a response back.
Execution Sample
Rest API
GET /users/123 HTTP/1.1
Host: api.example.com


Response:
HTTP/1.1 200 OK
Content-Type: application/json

{"id":123,"name":"Alice"}
A client requests user data with ID 123 and the server responds with user details in JSON.
Execution Table
StepActionRequest/Response ContentResult
1Client prepares GET requestGET /users/123 HTTP/1.1 Host: api.example.comRequest ready to send
2Client sends requestRequest sent to serverWaiting for response
3Server receives requestGET /users/123 HTTP/1.1Request accepted
4Server processes requestLook up user with ID 123User found: {"id":123,"name":"Alice"}
5Server sends responseHTTP/1.1 200 OK Content-Type: application/json {"id":123,"name":"Alice"}Response sent
6Client receives responseHTTP/1.1 200 OK Content-Type: application/json {"id":123,"name":"Alice"}Response received
7Client processes responseParse JSON bodyUser data available: id=123, name=Alice
8EndNo more actionsProcess complete
💡 Client has received and processed the server's response, completing the request cycle.
Variable Tracker
VariableStartAfter Step 4After Step 6Final
requestemptyGET /users/123 HTTP/1.1GET /users/123 HTTP/1.1sent
responseemptyemptyHTTP/1.1 200 OK with JSON bodyreceived and parsed
user_datanonenonenone{"id":123,"name":"Alice"}
Key Moments - 3 Insights
Why does the client wait after sending the request?
After sending the request (Step 2), the client waits because it needs the server to process the request and send back a response (see Step 3 and 4 in execution_table).
What does the server do when it receives the request?
The server looks up the requested data (user with ID 123) and prepares the response (Step 4 and 5 in execution_table).
How does the client use the response data?
The client parses the JSON response body to extract user details (Step 7 in execution_table), making the data usable in the application.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the content of the response at Step 6?
AHTTP/1.1 200 OK with JSON body {"id":123,"name":"Alice"}
BGET /users/123 HTTP/1.1 request
CEmpty response
DError message
💡 Hint
Check the 'Request/Response Content' column at Step 6 in execution_table.
At which step does the server find the user data?
AStep 2
BStep 4
CStep 6
DStep 7
💡 Hint
Look for 'Server processes request' in execution_table.
If the user ID was not found, how would the response change in the execution_table?
ARequest would not be sent
BResponse would be HTTP 200 OK with empty body
CResponse would be HTTP 404 Not Found
DClient would not receive any response
💡 Hint
Think about standard HTTP status codes for missing resources.
Concept Snapshot
Example requests and responses:
- Client sends HTTP request (GET, POST, etc.)
- Server processes and sends HTTP response
- Response includes status code and data (often JSON)
- Client parses response to use data
- Common status codes: 200 OK, 404 Not Found
- Requests and responses form the communication cycle
Full Transcript
This visual execution shows how a client sends an HTTP GET request to a server to get user data. The client prepares the request, sends it, and waits. The server receives the request, looks up the user with ID 123, and sends back a response with status 200 OK and JSON data. The client receives and parses this response to get the user details. This cycle is the basic pattern of REST API communication.

Practice

(1/5)
1.

What is the main purpose of an example request in a REST API?

GET /users/123

easy
A. To list all possible API endpoints
B. To display the server's internal code
C. To describe the server's hardware
D. To show how to ask the server for specific data or action

Solution

  1. Step 1: Understand what a request does

    A request is how a client asks a server for data or to perform an action.
  2. Step 2: Identify the role of example requests

    Example requests show exactly how to ask the server, like which URL and method to use.
  3. Final Answer:

    To show how to ask the server for specific data or action -> Option D
  4. Quick Check:

    Example request = How to ask server [OK]
Hint: Example requests show how to ask the server clearly [OK]
Common Mistakes:
  • Confusing requests with server code
  • Thinking examples list all endpoints
  • Mixing requests with server hardware info
2.

Which of the following is the correct syntax for a simple GET request example in REST API?

easy
A. GET /api/items HTTP/1.1\nHost: example.com
B. POST /api/items HTTP/1.1\nHost: example.com
C. FETCH /api/items HTTP/1.1\nHost: example.com
D. SEND /api/items HTTP/1.1\nHost: example.com

Solution

  1. Step 1: Identify the HTTP method for retrieving data

    GET is the standard method to request data from a server.
  2. Step 2: Check the syntax of the request line

    The correct format is "GET /path HTTP/1.1" followed by headers like Host.
  3. Final Answer:

    GET /api/items HTTP/1.1\nHost: example.com -> Option A
  4. Quick Check:

    GET method + correct syntax = GET /api/items HTTP/1.1\nHost: example.com [OK]
Hint: GET requests retrieve data; syntax starts with GET /path HTTP/1.1 [OK]
Common Mistakes:
  • Using POST instead of GET for data retrieval
  • Using non-standard methods like FETCH or SEND
  • Incorrect request line format
3.

Given this example response from a REST API:

{
  "id": 101,
  "name": "Alice",
  "active": true
}

What is the value of the active field?

medium
A. false
B. true
C. "true" (string)
D. null

Solution

  1. Step 1: Read the JSON response carefully

    The response shows "active": true without quotes, meaning a boolean true value.
  2. Step 2: Understand JSON data types

    Boolean true is a logical value, not a string or null.
  3. Final Answer:

    true -> Option B
  4. Quick Check:

    Boolean true in JSON = true [OK]
Hint: True without quotes means boolean true, not string [OK]
Common Mistakes:
  • Confusing boolean true with string "true"
  • Choosing false or null by mistake
  • Ignoring JSON syntax rules
4.

Look at this example request and response pair:

Request: POST /api/login HTTP/1.1\nHost: example.com\nContent-Type: application/json\n\n{"username": "user", "password": "pass"}
Response: 404 Not Found

What is the most likely error in the example?

medium
A. The URL path is incorrect or missing
B. The HTTP method POST is invalid
C. The Content-Type header is wrong
D. The JSON body is malformed

Solution

  1. Step 1: Understand 404 Not Found meaning

    404 means the server did not find the requested URL path.
  2. Step 2: Check the request details

    POST and Content-Type look correct; JSON body is valid. So the path is likely wrong or missing.
  3. Final Answer:

    The URL path is incorrect or missing -> Option A
  4. Quick Check:

    404 error = wrong URL path [OK]
Hint: 404 means URL path not found, check request URL [OK]
Common Mistakes:
  • Thinking 404 means bad JSON or headers
  • Assuming POST method is invalid
  • Ignoring the URL path correctness
5.

You want to create an example response for a REST API that returns a list of products with their id and price. Which JSON response below correctly shows two products?

hard
A. [{"id": 1, "price": "9.99"}, {"id": 2, "price": "19.99"}]
B. {"id": 1, "price": 9.99, "id": 2, "price": 19.99}
C. [{"id": 1, "price": 9.99}, {"id": 2, "price": 19.99}]
D. {"products": {"id": 1, "price": 9.99}, {"id": 2, "price": 19.99}}

Solution

  1. Step 1: Identify correct JSON array format

    A list of products should be an array (square brackets) containing objects for each product.
  2. Step 2: Check each option's JSON structure

    [{"id": 1, "price": 9.99}, {"id": 2, "price": 19.99}] is a valid JSON array with two objects, each with id and price as numbers. {"id": 1, "price": 9.99, "id": 2, "price": 19.99} is invalid JSON with duplicate keys. [{"id": 1, "price": "9.99"}, {"id": 2, "price": "19.99"}] uses price as strings, which is allowed but less ideal. {"products": {"id": 1, "price": 9.99}, {"id": 2, "price": 19.99}} has invalid JSON syntax.
  3. Final Answer:

    [{"id": 1, "price": 9.99}, {"id": 2, "price": 19.99}] -> Option C
  4. Quick Check:

    Array of objects with numeric price = [{"id": 1, "price": 9.99}, {"id": 2, "price": 19.99}] [OK]
Hint: Use JSON array with objects for multiple items [OK]
Common Mistakes:
  • Using duplicate keys in one object
  • Incorrect JSON syntax with braces and commas
  • Using strings for numeric values unnecessarily
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard