Junior
What is the difference between GET and POST methods in HTTP?
sobes.tech AI
Answer from AI
GET is used to request data from a server. Data is transmitted in the URL as query parameters. GET requests are idempotent and can be cached.
POST is used to send data to the server for creating or updating a resource. Data is transmitted in the request body. POST requests are not idempotent and are not cached by default.
| Feature | GET | POST |
|---|---|---|
| Purpose | Retrieve data | Send data (create/update) |
| Data | In URL (query parameters) | In request body |
| Idempotency | Yes | No |
| Caching | Yes | No (by default) |
| Security | Data visible in URL | Data not visible in URL |
| Data size | Limited by URL length | No strict limitations |
// Example GET request
fetch('/users?id=123')
.then(response => response.json())
.then(data => console.log(data));
// Example POST request
fetch('/users', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ name: 'John Doe', age: 30 })
})
.then(response => response.json())
.then(data => console.log(data));