Watch Me Code 2: Ways to send data over HTTP

Examples using the httpbin.org service

https://httpbin.org/


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]:
{'args': {'age': '45', 'name': 'mike'},
 'headers': {'Accept': '*/*',
  'Accept-Encoding': 'gzip, deflate',
  'Host': 'httpbin.org',
  'User-Agent': 'python-requests/2.22.0'},
 'origin': '71.176.79.102, 71.176.79.102',
 'url': 'https://httpbin.org/get?name=mike&age=45'}

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]:
{'args': {'age': '45', 'name': 'mike'},
 'headers': {'Accept': '*/*',
  'Accept-Encoding': 'gzip, deflate',
  'Host': 'httpbin.org',
  'User-Agent': 'python-requests/2.22.0'},
 'origin': '71.176.79.102, 71.176.79.102',
 'url': 'https://httpbin.org/get?name=mike&age=45'}

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]:
{'args': {},
 'headers': {'Accept': '*/*',
  'Accept-Encoding': 'gzip, deflate',
  'Api-Key': 'testing',
  'Host': 'httpbin.org',
  'Id': '345876',
  'User-Agent': 'python-requests/2.22.0'},
 'origin': '71.176.79.102, 71.176.79.102',
 'url': 'https://httpbin.org/get'}

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]:
{'args': {'age': '45', 'name': 'mike'},
 'headers': {'Accept': '*/*',
  'Accept-Encoding': 'gzip, deflate',
  'Api-Key': 'demo',
  'Host': 'httpbin.org',
  'User-Agent': 'python-requests/2.22.0'},
 'origin': '71.176.79.102, 71.176.79.102',
 'url': 'https://httpbin.org/get?name=mike&age=45'}

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]:
{'args': {},
 '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.this is a lot of data.',
 'files': {},
 'form': {},
 'headers': {'Accept': '*/*',
  'Accept-Encoding': 'gzip, deflate',
  'Content-Length': '154',
  'Host': 'httpbin.org',
  'User-Agent': 'python-requests/2.22.0'},
 'json': None,
 'origin': '71.176.79.102, 71.176.79.102',
 'url': 'https://httpbin.org/post'}

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]:
{'args': {},
 'data': '',
 'files': {},
 'form': {'age': '45', 'name': 'Mike', 'status': 'married'},
 'headers': {'Accept': '*/*',
  'Accept-Encoding': 'gzip, deflate',
  'Content-Length': '31',
  'Content-Type': 'application/x-www-form-urlencoded',
  'Host': 'httpbin.org',
  'User-Agent': 'python-requests/2.22.0'},
 'json': None,
 'origin': '71.176.79.102, 71.176.79.102',
 'url': 'https://httpbin.org/post'}

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]:
{'args': {'id': '1'},
 'data': '',
 'files': {},
 'form': {'age': '45', 'name': 'Mike', 'status': 'married'},
 'headers': {'Accept': '*/*',
  'Accept-Encoding': 'gzip, deflate',
  'Api-Key': '32871549',
  'Content-Length': '31',
  'Content-Type': 'application/x-www-form-urlencoded',
  'Host': 'httpbin.org',
  'User-Agent': 'demo'},
 'json': None,
 'origin': '71.176.79.102, 71.176.79.102',
 'url': 'https://httpbin.org/post?id=1'}

In [ ]: