from this video
httpbin.org —> more info about http
User requests library: pip3 install requests
import requests
r = requests.get('url')
print(help(r)) # gets help page, shows all functions
print(r) # gets status code
print(r.status_code) # gets status code
print(r.ok) # if this prints "True", means you got less than a 400 status code
The request we just made had a status code of 200
. Status codes are returned with every request that is made to a web server. Status codes indicate information about what happened with a request. Here are some codes that are relevant to GET requests:
200
— everything went okay, and the result has been returned (if any)301
— the server is redirecting you to a different endpoint. This can happen when a company switches domain names, or an endpoint name is changed.401
— the server thinks you’re not authenticated. This happens when you don’t send the right credentials to access an API (we’ll talk about authentication in a later post).400
— the server thinks you made a bad request. This can happen when you don’t send along the right data, among other things.403
— the resource you’re trying to access is forbidden — you don’t have the right permissions to see it.404
— the resource you tried to access wasn’t found on the server.downloading images
import requests
r = requests.get('<https://imgs.xkcd.com/comics/python.png>')
# creates a file called pythoncomic.png
# "wb" means write bytes since .content gives us back bytes
with open('pythoncomic.png','wb') as f:
f.write(r.content)
params
# dictionary of url parameters
payload = {'page': 2, 'count': 25}
r = requests.get('<https://httpbin.org/get>', params=payload)
print(r.text) #gets text of this, shows arguments of dictionary created
print(r.url) # shows url with query params, "<https://httpbin.org/get?page=2&count=25>"
delete - request's DELETE params