What is get and post method on API

#api

Answer

Overview

In REST APIs, GET and POST are the two most commonly used HTTP methods. They serve fundamentally different purposes.


GET Method

GET is used to retrieve data from the server. It does NOT modify any data.

dart
// GET — fetch list of users
final response = await http.get(
  Uri.parse('https://api.example.com/users'),
  headers: {'Authorization': 'Bearer $token'},
);
// Response body contains the data

GET with Query Parameters

dart
// GET with query params — filter users by city
final uri = Uri.parse('https://api.example.com/users')
    .replace(queryParameters: {'city': 'Chennai', 'limit': '10'});

final response = await http.get(uri);
// URL becomes: https://api.example.com/users?city=Chennai&limit=10

POST Method

POST is used to send data to the server — to create a new resource.

dart
// POST — create a new user
final response = await http.post(
  Uri.parse('https://api.example.com/users'),
  headers: {'Content-Type': 'application/json'},
  body: jsonEncode({
    'name': 'Alice',
    'email': 'alice@example.com',
    'age': 28,
  }),
);

Key Differences

FeatureGETPOST
PurposeRead / fetch dataCreate / send data
Body❌ No body✅ Has request body
URL parameters✅ Yes (query params)❌ Not typically
Cached✅ Yes (by browsers)❌ No
Idempotent✅ Yes (same result each time)❌ No (creates new record each time)
Bookmarkable✅ Yes❌ No
Security⚠️ Data visible in URL✅ Data in body (use HTTPS)
Size limitLimited by URL lengthNo practical limit

All HTTP Methods Overview

MethodPurposeIdempotent
GETRead resource
POSTCreate resource
PUTReplace resource entirely
PATCHPartially update resource
DELETEDelete resource

REST Conventions

text
GET    /users          → Get all users
GET    /users/123      → Get user with id 123
POST   /users          → Create a new user
PUT    /users/123      → Replace user 123 entirely
PATCH  /users/123      → Update specific fields of user 123
DELETE /users/123      → Delete user 123

Status Codes

CodeMeaningTypically Used With
200 OKSuccessGET, PUT, PATCH
201 CreatedResource createdPOST
204 No ContentSuccess, no bodyDELETE
400 Bad RequestInvalid inputAny
401 UnauthorizedAuth requiredAny
404 Not FoundResource not foundGET, PUT, DELETE
500 Server ErrorServer issueAny

Rule: GET = reading, POST = creating. Always use HTTPS to protect data in transit regardless of the HTTP method.