GET Requests
Table of Contents
Understanding GET Requests
In REST APIs, GET is the most common HTTP method. It is used to retrieve data from the server without changing anything on the server.
This chapter focuses only on GET in the context of REST, not on other HTTP methods or full REST design, which are covered in separate chapters.
Key idea:
GET requests retrieve data and must not change server state.
They should be safe and idempotent.
What a GET Request Is For
A GET request asks the server:
"Give me the representation of this resource."
Examples of what you might fetch with GET:
- A single user by ID
- A list of products
- The details of a single product
- A list of orders for a user
- A search result for
?q=shoes
In REST, typical GET endpoints might look like:
GET /usersGET /users/42GET /productsGET /products/123GET /orders?status=pending
In all these examples, you are reading information.
Typical RESTful GET Patterns
| Endpoint | Meaning |
|---|---|
GET /resources | List or search resources |
GET /resources/{id} | Get one resource by ID |
GET /users/me | Get information about the current authenticated user |
GET /stats | Read-only statistics or summary information |
Where GET Data Goes: URL, Path, Query
A GET request does not usually send a request body in REST APIs. Data is typically sent in:
- The path
- Example:
/users/42 42is a path parameter.- The query string
- Example:
/users?country=US&status=active&page=2 country,status,pageare query parameters.
Example: Path vs Query in GET
GET /users/42- Path parameter:
42represents the resource ID GET /users?country=US&status=active&page=2- Query parameters:
country = USstatus = activepage = 2
Typical usage:
- Path parameters: identify a single specific resource
- Query parameters: filter, search, sort, and paginate lists
You will see path parameters and query parameters in much more detail in dedicated chapters, but for GET requests the key idea is:
Rule:
Use the path to point at what you want, and use the query string to refine how you want it.
GET Request Examples
Simple GET Request
Request:
GET /users/42 HTTP/1.1
Host: api.example.com
Accept: application/jsonTypical response:
HTTP/1.1 200 OK
Content-Type: application/json
{
"id": 42,
"name": "Alice",
"email": "alice@example.com"
}GET With Query Parameters
Request:
GET /products?category=books&sort=price_asc&page=1&limit=10 HTTP/1.1
Host: api.example.com
Accept: application/jsonTypical response:
HTTP/1.1 200 OK
Content-Type: application/json
{
"items": [
{
"id": 101,
"title": "Learn Backend Development",
"price": 29.99
},
{
"id": 102,
"title": "Advanced REST APIs",
"price": 34.99
}
],
"page": 1,
"limit": 10,
"total": 57
}Here the query parameters control filtering, sorting, and pagination, which are covered more deeply in later chapters.
GET and RESTful URLs
In the chapter on RESTful URL design you will see general rules, but here are examples focused only on GET:
| Operation | Recommended GET URL |
|---|---|
| List all users | GET /users |
| Get one user | GET /users/{user_id} |
| Get all posts for a user | GET /users/{user_id}/posts |
| Get one post of a user | GET /users/{user_id}/posts/{post_id} |
| Search users by name and country | GET /users?name=alice&country=US |
| Get paginated list of products | GET /products?page=2&limit=50 |
| Get filtered, sorted products | GET /products?category=shoes&sort=-price |
Notice that all of these are reads. No creation, updates, or deletions are done with GET.
Safety and Idempotency of GET
Two important properties of GET:
Safe
A method is safe if it does not change server state.
GET should be used only for reading. For example:
- Reading products: safe
- Getting user profile: safe
- Triggering a payment: not safe for GET
If a GET request changes your database, that is usually a design problem.
Idempotent
A method is idempotent if making the same request many times has the same effect as making it once.
For GET:
- First GET
/users/42retrieves user 42 - The second, third, and 100th GET
/users/42do the same thing - There is no additional change caused by repeating the request
Design rule:
GET must be safe and idempotent. Do not use GET to create, modify, or delete data.
GET Request Body: Should You Use It?
The HTTP standard allows a body in a GET request, but in practice:
- Many servers and frameworks ignore the body on GET
- Caches and proxies may ignore it
- Developer tools often do not handle it well
In REST APIs for backend development, it is a strong convention to not use a body for GET.
Use:
- Path and query parameters for filters, options, and identifiers
- POST, if you truly need a complex body and cannot express it as query parameters
GET and Caching
GET works very well with caching, which is one reason it must be safe.
Because GET does not modify data, intermediate systems such as:
- Browsers
- Reverse proxies
- CDNs
can cache responses and reuse them.
You will learn HTTP caching in detail in a separate chapter, but for GET:
- A
GET /productsresponse can be cached for a short time - If another client asks for the same URL, the cache can return the stored response instead of hitting your backend again
Example:
GET /products/123 HTTP/1.1
Host: api.example.com
Accept: application/jsonResponse:
HTTP/1.1 200 OK
Content-Type: application/json
Cache-Control: max-age=60
{
"id": 123,
"name": "Wireless Mouse",
"price": 19.99
}
The header Cache-Control: max-age=60 tells caches they can reuse this response for 60 seconds.
Practical hint:
If your endpoint changes data, do not use GET. If something uses GET, design it so it can be safely cached.
Common GET Status Codes
When you handle GET requests in a REST API, you usually return:
| Status code | Meaning | When to use for GET |
|---|---|---|
200 OK | Success with a response body | Resource found and returned |
204 No Content | Success with no body | Request succeeded but nothing to return |
301/302 | Redirect | Resource moved or redirect to another URL |
400 Bad Request | Client error, invalid input | Query parameters invalid |
401 Unauthorized | Not authenticated | Authentication required |
403 Forbidden | Not allowed | Authenticated but not allowed |
404 Not Found | Resource does not exist | No resource with that ID or URL |
500 Internal Server Error | Server error | Something went wrong on the server |
You will study status codes more deeply in the dedicated chapter, but for GET it is helpful to recognize:
200when data is returned404when the resource is missing400when the query is invalid
Example:
GET /users/999999 HTTP/1.1
Host: api.example.com
Accept: application/jsonHTTP/1.1 404 Not Found
Content-Type: application/json
{
"detail": "User not found"
}GET in a Simple REST API: Practical Examples
To make GET concrete, here are example designs for a small REST API.
1. Users API
| Operation | Method | URL | Request body | Response body |
|---|---|---|---|---|
| List users | GET | /users | None | List of users |
| Get user by ID | GET | /users/{id} | None | Single user |
| Search users by name | GET | /users?name=… | None | Filtered list |
Example requests:
GET /users HTTP/1.1
Host: api.example.com
Accept: application/jsonGET /users/42 HTTP/1.1
Host: api.example.com
Accept: application/jsonGET /users?name=alice&page=2 HTTP/1.1
Host: api.example.com
Accept: application/json2. Products API
| Operation | Method | URL |
|---|---|---|
| List products | GET | /products |
| Filter products by category | GET | /products?category=electronics |
| Filter and sort | GET | /products?category=electronics&sort=price_desc |
| Get single product | GET | /products/{product_id} |
| Get reviews for a product | GET | /products/{product_id}/reviews |
Example GET for a list:
GET /products?category=electronics&sort=price_desc&page=1&limit=20 HTTP/1.1
Host: api.example.com
Accept: application/jsonExample GET for a single resource:
GET /products/987 HTTP/1.1
Host: api.example.com
Accept: application/jsonCommon Mistakes to Avoid with GET
1. Using GET to Modify Data
Bad design:
GET /create-user?name=aliceGET /delete-post?id=123
These change data but use GET.
Better:
POST /userswith a JSON body to create a userDELETE /posts/123to delete a post
2. Putting Sensitive Data in the URL
Avoid sending sensitive information in query parameters, for example:
GET /login?email=alice@example.com&password=secretProblems:
- URLs can be logged by servers and proxies
- Browser history stores them
- They can appear in analytics logs
Sensitive data such as passwords, tokens, or personal information should not be sent in GET query strings.
3. Overly Complex Query Parameters
Sometimes people try to encode very complex filters in a single query string parameter, for example:
GET /search?filter=(status='active' AND age>18) OR city='London'This becomes hard to parse and maintain. In many simple cases you can split it:
GET /users?status=active&min_age=18&city=LondonFor very complex queries you might prefer:
- A POST endpoint with a JSON body, or
- A dedicated search service, or
- Simpler filter rules
How GET Is Used by Browsers and Tools
Browsers
When you:
- Type a URL in the address bar
- Click a regular link
- Refresh a page
the browser sends a GET request.
Example:
You enter https://example.com/products?category=books
The browser sends:
GET /products?category=books HTTP/1.1
Host: example.comHTML and GET
An HTML form without a method uses GET by default:
<form action="/search">
<input name="q" placeholder="Search">
<button type="submit">Search</button>
</form>If you type "backend" and submit, the browser sends:
GET /search?q=backend HTTP/1.1
Host: example.comThis is why search engines often use URLs like:
https://www.google.com/search?q=backend+development
Designing Good GET Endpoints in REST APIs
When you design GET endpoints for a REST API, keep these practical guidelines in mind:
- Use nouns, not verbs, in URLs
- Good:
GET /users/42 - Bad:
GET /getUser?id=42 - Use plural resource names for collections
GET /usersGET /products- Support filtering and pagination with query parameters
GET /products?category=books&page=2&limit=20- Return appropriate status codes
200 OKwhen data is found404 Not Foundwhen the resource does not exist400 Bad Requestwhen parameters are invalid- Do not require a body
- All needed input should be in the path or query string
- Keep them read-only
- Never create, update, or delete data with GET
Summary
- GET is used to retrieve representations of resources.
- It should be safe and idempotent, and must not change server state.
- Data for GET requests should be passed via the path and query string, not in the body.
- GET works very well with caching.
- Use GET with clear, noun-based RESTful URLs, for example:
GET /usersGET /users/{id}GET /products?category=books&page=1
In later chapters you will learn in more detail how GET interacts with pagination, filtering, sorting, and how to implement GET endpoints in a specific framework such as FastAPI.
Views: 7
KAHIBARO