Надсилання HTTP-запиту

Python:
Надсилання HTTP-запиту

How to: (Як це зробити:)

import requests

# Get request
response = requests.get('https://api.github.com')
print(response.status_code)
print(response.json())

# Post request
data = {'key': 'value'}
response = requests.post('https://httpbin.org/post', json=data)
print(response.status_code)
print(response.json())

Output:

200
{'current_user_url': 'https://api.github.com/user', ...}

200
{
  "args": {}, 
  "data": "{\"key\": \"value\"}", 
  "files": {}, 
  "form": {}, 
  "headers": {
    ...
  }, 
  ...
}

Deep Dive (Поглиблений Аналіз)

HTTP requests have been around since the early ’90s, an essential part of web browsing. Python’s requests library makes requests easy: you use get to fetch data and post to send it. There are alternatives, like http.client for lower-level operations and the urllib library, but they are more verbose and complex. requests handles many implementation details like encoding parameters and managing responses.

See Also (Дивіться також)