0
0
Rest APIprogramming~5 mins

GET for reading resources in Rest API

Choose your learning style9 modes available
Introduction

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.