Sobes.tech
Middle+

What have you worked with regarding API in Python? What HTTP request methods do you know? How does PUT differ from POST?

sobes.tech AI

Answer from AI

In Python, libraries like requests are often used for working with APIs. They allow sending various types of HTTP requests.

Main HTTP methods:

  • GET — retrieve data from the server;
  • POST — send data to the server to create a resource;
  • PUT — replace or create a resource at a specified URL;
  • DELETE — delete a resource;
  • PATCH — partially update a resource.

Difference between PUT and POST:

  • POST is usually used to create a new resource. POST requests can create multiple resources and are not idempotent (repeating the request may create new resources).

  • PUT is used to create or replace a resource at a specific URL. PUT is idempotent — repeating the same request with the same data will not change the result.

Example of a request with requests:

import requests

# GET request
response = requests.get('https://api.example.com/items')

# POST request
data = {'name': 'item1'}
response = requests.post('https://api.example.com/items', json=data)

# PUT request
update_data = {'name': 'updated item'}
response = requests.put('https://api.example.com/items/1', json=update_data)