Junior — Middle
Can you list the main HTTP methods and their functions?
sobes.tech AI
Answer from AI
Main HTTP methods and their functions:
- GET — requests data from the server. Used to retrieve information without changing the state.
- POST — sends data to the server to create a resource or perform an action.
- PUT — updates an existing resource or creates it if it does not exist.
- DELETE — deletes the specified resource on the server.
- PATCH — partially updates a resource, changing only specified fields.
- HEAD — requests only the response headers, without the body, to check metadata.
Example of using GET and POST in Python with requests:
import requests
response = requests.get('https://api.example.com/items')
print(response.json())
response = requests.post('https://api.example.com/items', json={'name': 'NewItem'})
print(response.status_code)