The GET method is used to ask a server to send back data. It helps you read or view information from a website or app.
When you want to see a list of items, like all books in a library app.
When you want to view details about one specific item, like a user profile.
When you want to load data to show on a webpage, like news articles.
When you want to check the status or information without changing anything.
When you want to fetch data for search results or filters.
Syntax
Rest API
GET /resource-path HTTP/1.1
Host: example.com
The GET request asks the server to send data located at the given resource path.
GET requests do not change data on the server; they only read it.
Examples
This asks the server for a list of all users.
Rest API
GET /users HTTP/1.1
Host: api.example.com
This asks the server for details about the user with ID 123.
Rest API
GET /users/123 HTTP/1.1
Host: api.example.com
This asks for products filtered by the category 'books'.
Rest API
GET /products?category=books HTTP/1.1
Host: shop.example.com
Sample Program
This Python program uses the GET method to read a post from a test API. It prints the post data if successful.
Rest API
import requests
response = requests.get('https://jsonplaceholder.typicode.com/posts/1')
if response.status_code == 200:
print(response.json())
else:
print('Failed to get data')
OutputSuccess
Important Notes
GET requests should never change data on the server.
Data sent in GET requests is visible in the URL, so avoid sending sensitive info this way.
Servers usually respond with status code 200 if the GET request is successful.
Summary
GET is used to read or fetch data from a server.
It does not change or delete any data.
GET requests include the resource path and optional query parameters.
Practice
(1/5)
1. What is the main purpose of a GET request in REST API?
easy
A. To read or fetch data from the server
B. To create new data on the server
C. To update existing data on the server
D. To delete data from the server
Solution
Step 1: Understand the HTTP methods
GET is one of the HTTP methods used in REST APIs. It is specifically designed to retrieve data without changing it.
Step 2: Identify the purpose of GET
GET requests fetch or read data from the server without modifying it, unlike POST, PUT, or DELETE.
Final Answer:
To read or fetch data from the server -> Option A
Quick Check:
GET = Read data [OK]
Hint: GET always fetches data without changing it [OK]
Common Mistakes:
Confusing GET with POST which creates data
Thinking GET deletes data
Assuming GET updates data
2. Which of the following is the correct syntax for a GET request URL to fetch a user with ID 5?
easy
A. POST /users/5
B. POST /users?id=5
C. GET /users/5
D. DELETE /users/5
Solution
Step 1: Identify the HTTP method for reading
GET is the correct method to read or fetch data, so the method must be GET.
Step 2: Check the URL path for resource
/users/5 is the typical RESTful path to get a user with ID 5. Other options use wrong HTTP methods.
Final Answer:
GET /users/5 -> Option C
Quick Check:
GET + resource path = correct syntax [OK]
Hint: GET method + resource path fetches data [OK]
Common Mistakes:
Using POST or DELETE instead of GET
Using query parameters incorrectly
Mixing HTTP methods and URLs
3. Given the following GET request URL: /products?category=books&limit=3, what does the server most likely return?
medium
A. All products without filtering
B. Products with category 'limit' and quantity 3
C. An error because of invalid query parameters
D. First 3 products in the books category
Solution
Step 1: Analyze the query parameters
The URL has two query parameters: category=books and limit=3, which filter products by category and limit the number returned.
Step 2: Understand typical API behavior
APIs commonly use 'category' to filter items and 'limit' to restrict the number of results returned.
Final Answer:
First 3 products in the books category -> Option D