3.14. HTTP using requests
¶
3.14.1. Basic API¶
import requests
requests.get('https://httpbin.org/get') # <Response [200]>
requests.post('https://httpbin.org/post', data={'key':'value'}) # <Response [200]>
requests.put('https://httpbin.org/put', data={'key':'value'}) # <Response [200]>
requests.delete('https://httpbin.org/delete') # <Response [200]>
requests.head('https://httpbin.org/get') # <Response [200]>
requests.options('https://httpbin.org/get') # <Response [200]>
3.14.2. Response¶
from http import HTTPStatus
import requests
response = requests.get('https://httpbin.org/get')
# <Response [200]>
if response.status_code == HTTPStatus.OK:
data = response.raw # Raw Response Content
data = response.content # Binary Response Content
data = response.text # Response Content
data = response.json() # JSON Response Content
print(data)
3.14.3. GET Requests¶
params
from http import HTTPStatus
import requests
response = requests.get('https://httpbin.org/get')
# <Response [200]>
if response.status_code == HTTPStatus.OK:
data = response.json()
print(data)
from http import HTTPStatus
import requests
params = {'key1': 'value1', 'key2': 'value2'}
response = requests.get('https://httpbin.org/get', params=params)
# <Response [200]>
print(response.url)
# https://httpbin.org/get?key2=value2&key1=value1
if response.status_code == HTTPStatus.OK:
data = response.json()
print(data)
from http import HTTPStatus
import requests
data = {'key1': 'value1', 'key2': ['value2', 'value3']}
response = requests.get('https://httpbin.org/get', params=data)
# <Response [200]>
print(response.url)
# https://httpbin.org/get?key1=value1&key2=value2&key2=value3
if response.status_code == HTTPStatus.OK:
data = response.json()
print(data)
3.14.4. POST Requests¶
3.14.5. POST Request with data¶
from http import HTTPStatus
import requests
data = {'firstname': 'Mark', 'lastname': 'Watney'}
response = requests.post('https://httpbin.org/post', data=data)
# <Response [200]>
if response.status_code == HTTPStatus.OK:
print('Created')
from http import HTTPStatus
import requests
data = {'key1': ['value1', 'value2']}
response = requests.post('https://httpbin.org/post', data=data)
# <Response [200]>
print(response.text)
# {
# ...
# "form": {
# "key1": [
# "value1",
# "value2"
# ]
# },
# ...
# }
if response.status_code == HTTPStatus.OK:
print('Created')
3.14.6. POST Request with JSON¶
import json
from http import HTTPStatus
import requests
data = {
'firstname': 'José',
'lastname': 'Jiménez',
}
response = requests.post('https://httpbin.org/post', data=json.dumps(data))
# <Response [200]>
if response.status_code == HTTPStatus.OK:
print('Created')
import json
from http import HTTPStatus
import requests
data = {
'firstname': 'José',
'lastname': 'Jiménez',
}
response = requests.post('https://httpbin.org/post', json=data)
# <Response [200]>
if response.status_code == HTTPStatus.OK:
print('Created')
3.14.7. DELETE Requests¶
import requests
from http import HTTPStatus
response = requests.delete('https://httpbin.org/delete')
# <Response [200]>
if response.status_code == HTTPStatus.OK:
print('Deleted')
3.14.8. Custom Headers¶
import requests
from http import HTTPStatus
headers = {
'User-Agent': 'Python requests'
}
response = requests.get('https://httpbin.org/post', headers=headers)
# <Response [200]>
if response.status_code == HTTPStatus.OK:
data = response.json()
print(data)
response.headers
# {
# 'content-encoding': 'gzip',
# 'transfer-encoding': 'chunked',
# 'connection': 'close',
# 'server': 'nginx/1.0.4',
# 'x-runtime': '148ms',
# 'etag': '"e1ca502697e5c9317743dc078f67693f"',
# 'content-type': 'application/json'
# }
response.headers['Content-Type']
# 'application/json'
response.headers.get('content-type')
# 'application/json'
3.14.9. Timeout¶
import requests
requests.get('https://httpbin.org/get', timeout=0.001)
# Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
# requests.exceptions.Timeout: HTTPConnectionPool(host='httpbin.org', port=80): Request timed out. (timeout=0.001)
3.14.10. Basic Auth¶
import requests
from http import HTTPStatus
response = requests.get('https://api.github.com/users', auth=('login', 'password'))
# <Response [200]>
if response.status_code == HTTPStatus.OK:
data = response.json()
print(data)
3.14.11. Requests OAuth¶
$ pip install requests_oauthlib
from requests_oauthlib import OAuth2Session
from flask import Flask, request, redirect, session, url_for
from flask.json import jsonify
# This information is obtained upon registration of a new GitHub
client_id = "<your client key>"
client_secret = "<your client secret>"
authorization_base_url = 'https://github.com/login/oauth/authorize'
token_url = 'https://github.com/login/oauth/access_token'
@app.route("/login")
def login():
github = OAuth2Session(client_id)
authorization_url, state = github.authorization_url(authorization_base_url)
# State is used to prevent CSRF, keep this for later.
session['oauth_state'] = state
return redirect(authorization_url)
@app.route("/callback")
def callback():
github = OAuth2Session(client_id, state=session['oauth_state'])
token = github.fetch_token(token_url, client_secret=client_secret,
authorization_response=request.url)
return jsonify(github.get('https://api.github.com/user').json())
3.14.12. Assignments¶
3.14.12.1. REST API¶
Assignment: REST API
Complexity: medium
Lines of code: 35 lines
Time: 21 min
- English:
Create a free account on Github and confirm your email
Go to website http://github.com/settings/tokens
Generate a token in your profile (scope
public_repo
- Access public repositories)Using the
requests
libraryGet information about the user's repositories Django on https://github.com
Authenticate each request using Basic Auth and your Access Token
Then browse the list from Python and find the URL for the
django
repositoryBrowse this repository and its list of commits
Give the date and description of the last commit
Find the ID numbers of tickets (
Fixed #...
) from the issue tracker that have been resolved in the last monthRun doctests - all must succeed
- Polish:
Załóż darmowe konto na Github i potwierdź email
Wejdź na stronę internetową https://github.com/settings/tokens
Wygeneruj w swoim profilu token (scope
public_repo
- Access public repositories)Używając biblioteki
requests
Zaciągnij informacje o repozytoriach użytkownika Django na https://github.com
Każdy request uwierzytelnij za pomocą Basic Auth i swojego Access Tokena
Następnie przeglądnij listę z poziomu Pythona i znajdź URL dla repozytorium
django
Przeglądnij to repozytorium i jego listę komitów
Podaj datę i opis ostatniego komita
Znajdź numery ID tiketów (
Fixed #...
) z issue trackera, które zostały rozwiązane w ostatnim miesiącuUruchom doctesty - wszystkie muszą się powieść
- Hints:
$ curl -X GET https://api.github.com/orgs/django/repos $ curl -X GET https://api.github.com/repos/django/django/commits
... "name": "django", "fullname": "django/django", ... # wyszukaj "commits_url"