Basic requests

  • Importing the ‘requests’ library
import requests
  • GET request
requests.get(<URI>)
  • POST request
requests.post(<URI>,<DATA>)
  • PUT request
requests.put(<URI>,<DATA>)
  • DELETE request
requests.delete(<URI>)

Adding custom parameters

  • GET request with custom parameters
payload = {'key1': 'value1', 'key2': 'value2'}
r = requests.get(<URI>, params=payload)

Adding custom headers

  • GET request with custom headers
url = 'https://api.github.com/some/endpoint'
customHeaders = {'user-agent': 'my-app/0.0.1'}
r = requests.get(<URI>, headers = customHeaders)

Read received data

  • GET request with a JSON response
r = requests.get('https://api.github.com/events')
jsonResponse = r.json()
print(jsonResponse[<JSONFIELD>])

Source