Junior — Middle
Could you explain what methods of the HTTP protocol exist and what they are used for?
sobes.tech AI
Answer from AI
HTTP (HyperText Transfer Protocol) uses several main methods, each designed for a specific action with resources on the server:
- GET — requests a representation of the resource. Used to retrieve data without changing the server's state.
- POST — sends data to the server to create a new resource or perform processing.
- PUT — replaces the current resource on the server with the provided content.
- DELETE — deletes the specified resource.
- HEAD — requests only the headers of the resource, without the body, useful for checking existence or metadata.
- OPTIONS — returns the methods supported by the server for the resource.
- PATCH — partially updates the resource, changing only specified fields.
Example of a GET request in Python using the requests library:
import requests
response = requests.get('https://api.example.com/data')
print(response.status_code)
print(response.json())