James's Ramblings

Python: REST

Created: May 29, 2020

Imports

  • requests: for HTTP requests.
  • json: for manipulating JSON.
  • urllib.parse?

GET

import requests
response = requests.get('https://google.com/')
print(response)
>> <Response [200]>
print(response.status_code)
>>> 200
print(response.text)
  • Prints the JSON reply as text.
if response:
  print('Request is successful.')
else:
  print('Request returned an error.')
  • Will evaluate to True if the status code was between 200 and 400, and False otherwise.

JSON Responses

  • The requests library has a .json() method that converts JSON to a Python object.
print(response.json())