In [1]:
import requests
In [2]:
# In the URL query string, HTTP GET
url = "https://httpbin.org/get?name=mike&age=45"
response = requests.get(url)
response.json()
Out[2]:
In [3]:
# same example but don't create the querystring by hand. use a dict
querystring = { 'name' : 'mike', 'age' : 45 }
url = "http://httpbin.org/get"
response = requests.get(url, params = querystring)
response.json()
Out[3]:
In [8]:
# make a request, adding data to the header
# NOTE: all header values MUST be strings!!! AND they are case-insensitive as per the HTTP protocol spec.
header = { 'api-key' : 'testing', 'id' : '345876' }
url = "http://httpbin.org/get"
response = requests.get(url, headers = header)
response.json()
Out[8]:
In [9]:
# here's a combination of querystring plus headers:
querystring = { 'name' : 'mike', 'age' : 45 }
header = { 'api-key' : 'demo'}
url = "http://httpbin.org/get"
response = requests.get(url, params = querystring, headers = header)
response.json()
Out[9]:
In [13]:
# here's an example of a post
payload = "this is a lot of data.this is a lot of data.this is a lot of data.this is a lot of data.this is a lot of data.this is a lot of data.this is a lot of data."
url = "http://httpbin.org/post"
response = requests.post(url, data = payload)
response.json()
Out[13]:
In [14]:
# here's another post, with a python dict, because there are key-values the post uses form.
person = { 'name' : 'Mike', 'age' : 45, 'status' : 'married' }
url = "http://httpbin.org/post"
response = requests.post(url, data = person)
response.json()
Out[14]:
In [15]:
# this one uses a POST, payload, querystring and headers to show these can all be combined!
person = { 'name' : 'Mike', 'age' : 45, 'status' : 'married' }
header = { 'api-key' : '32871549', 'user-agent' : 'demo' }
querystring = { 'id' : 1 }
url = "http://httpbin.org/post"
response = requests.post(url, data = person, params = querystring, headers=header )
response.json()
Out[15]:
In [ ]: