Example requests and responses show how to talk to a web service and what answers to expect. They help you understand how to use the service step-by-step.
Example requests and responses in Rest API
GET /resource HTTP/1.1 Host: example.com Response: HTTP/1.1 200 OK Content-Type: application/json {"key": "value"}
The request line shows the method (GET) and the resource path (/resource).
The response includes a status code (200 OK) and data in JSON format.
GET /users HTTP/1.1 Host: api.example.com Response: HTTP/1.1 200 OK Content-Type: application/json [{"id":1,"name":"Alice"},{"id":2,"name":"Bob"}]
POST /login HTTP/1.1 Host: api.example.com Content-Type: application/json {"username":"user1","password":"pass123"} Response: HTTP/1.1 200 OK Content-Type: application/json {"token":"abc123xyz"}
DELETE /items/5 HTTP/1.1 Host: api.example.com Response: HTTP/1.1 204 No Content
This program sends a GET request to get users, then prints each user's id and name if successful.
import requests # Example: Get user data from API response = requests.get('https://api.example.com/users') if response.status_code == 200: users = response.json() for user in users: print(f"User {user['id']}: {user['name']}") else: print(f"Failed to get users: {response.status_code}")
Always check the response status code to know if the request worked.
Requests and responses usually use JSON format for easy reading and writing.
Headers like Content-Type tell the server what kind of data you send or expect.
Example requests show how to ask a web service for data or actions.
Example responses show what the server sends back after your request.
Using examples helps you learn and test APIs quickly and clearly.
Practice
What is the main purpose of an example request in a REST API?
GET /users/123
Solution
Step 1: Understand what a request does
A request is how a client asks a server for data or to perform an action.Step 2: Identify the role of example requests
Example requests show exactly how to ask the server, like which URL and method to use.Final Answer:
To show how to ask the server for specific data or action -> Option DQuick Check:
Example request = How to ask server [OK]
- Confusing requests with server code
- Thinking examples list all endpoints
- Mixing requests with server hardware info
Which of the following is the correct syntax for a simple GET request example in REST API?
Solution
Step 1: Identify the HTTP method for retrieving data
GET is the standard method to request data from a server.Step 2: Check the syntax of the request line
The correct format is "GET /path HTTP/1.1" followed by headers like Host.Final Answer:
GET /api/items HTTP/1.1\nHost: example.com -> Option AQuick Check:
GET method + correct syntax = GET /api/items HTTP/1.1\nHost: example.com [OK]
- Using POST instead of GET for data retrieval
- Using non-standard methods like FETCH or SEND
- Incorrect request line format
Given this example response from a REST API:
{
"id": 101,
"name": "Alice",
"active": true
}What is the value of the active field?
Solution
Step 1: Read the JSON response carefully
The response shows "active": true without quotes, meaning a boolean true value.Step 2: Understand JSON data types
Boolean true is a logical value, not a string or null.Final Answer:
true -> Option BQuick Check:
Boolean true in JSON = true [OK]
- Confusing boolean true with string "true"
- Choosing false or null by mistake
- Ignoring JSON syntax rules
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?
Solution
Step 1: Understand 404 Not Found meaning
404 means the server did not find the requested URL path.Step 2: Check the request details
POST and Content-Type look correct; JSON body is valid. So the path is likely wrong or missing.Final Answer:
The URL path is incorrect or missing -> Option AQuick Check:
404 error = wrong URL path [OK]
- Thinking 404 means bad JSON or headers
- Assuming POST method is invalid
- Ignoring the URL path correctness
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?
Solution
Step 1: Identify correct JSON array format
A list of products should be an array (square brackets) containing objects for each product.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.Final Answer:
[{"id": 1, "price": 9.99}, {"id": 2, "price": 19.99}] -> Option CQuick Check:
Array of objects with numeric price = [{"id": 1, "price": 9.99}, {"id": 2, "price": 19.99}] [OK]
- Using duplicate keys in one object
- Incorrect JSON syntax with braces and commas
- Using strings for numeric values unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
