The requests module of Python is one of the most common libraries to be used as an HTTP client.
httpbin.org is a nice service that you can use to try various HTTP requests.
In this articel I have collected a couple of the examples written using Python requests.
HTTP GET
examples/python/httpbin/get.py
import requests
res = requests.get('http://httpbin.org/get?name=Foo')
print(res.status_code)
print(res.reason)
print()
print("Headers:")
for key, value in res.headers.items():
print(f"{key} {value}")
print()
#print(res.content)
for key, value in res.json().items():
if key == "headers":
continue
print(f"{key} {value}")
The output will look like this:
examples/python/httpbin/get.out
200
OK
Headers:
Date Mon, 20 Apr 2020 12:15:08 GMT
Content-Type application/json
Content-Length 336
Connection keep-alive
Server gunicorn/19.9.0
Access-Control-Allow-Origin *
Access-Control-Allow-Credentials true
args {'name': 'Foo'}
origin 84.108.218.54
url http://httpbin.org/get?name=Foo
HTTP POST
examples/python/httpbin/post.py
import requests
res = requests.post('http://httpbin.org/post', data={"name": "Foo Bar"})
print(res.status_code)
print(res.reason)
print()
print("Headers:")
for key, value in res.headers.items():
print(f"{key} {value}")
print()
#print(res.content)
for key, value in res.json().items():
if key == "headers":
continue
print(f"{key} {value}")
The output will look like this:
examples/python/httpbin/post.out
200
OK
Headers:
Date Mon, 20 Apr 2020 12:15:02 GMT
Content-Type application/json
Content-Length 482
Connection keep-alive
Server gunicorn/19.9.0
Access-Control-Allow-Origin *
Access-Control-Allow-Credentials true
args {}
data
files {}
form {'name': 'Foo Bar'}
json None
origin 84.108.218.54
url http://httpbin.org/post
Basic Authentication
examples/python/httpbin/basic_auth.py
import requests
# curl -i --user foo:bar https://httpbin.org/basic-auth/foo/bar
res = requests.get('http://httpbin.org/basic-auth/foo/bar', auth=('foo', 'bar'))
print(res.status_code)
print(res.reason)
print()
for key, value in res.headers.items():
print(f"{key} {value}")
print()
print(res.content)
# for key, value in res.json().items():
# print(f"{key} {value}")
What happens if you don't provide the credentials:
examples/python/httpbin/basic_auth_no_credentials.py
import requests
res = requests.get('http://httpbin.org/basic-auth/foo/bar')
print(res.status_code)
print(res.reason)
print()
for key, value in res.headers.items():
print(f"{key} {value}")
What happens if you provide the wrong credentials?
examples/python/httpbin/basic_auth_wrong_credentials.py
import requests
res = requests.get('http://httpbin.org/basic-auth/foo/bar', auth=('foo', 'hello'))
print(res.status_code)
print(res.reason)
print()
for key, value in res.headers.items():
print(f"{key} {value}")