August 2018
Intermediate to advanced
366 pages
10h 14m
English
The steps for this recipe are as follows:
import urllib.request
import urllib.parse
import json
def http_request(url, query=None, method=None, headers={}, data=None):
"""Perform an HTTP request and return the associated response."""
parts = vars(urllib.parse.urlparse(url))
if query:
parts['query'] = urllib.parse.urlencode(query)
url = urllib.parse.ParseResult(**parts).geturl()
r = urllib.request.Request(url=url, method=method, headers=headers, data=data) with urllib.request.urlopen(r) as resp: msg, resp = resp.info(), resp.read() if msg.get_content_type() == 'application/json': ...