10 examples of 'python requests response json' in Python

Every line of 'python requests response json' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
90def _response_as_json(response: requests.Response) -> Dict[Text, Any]:
91 """Convert a HTTP response to json, raise exception if response failed."""
92
93 response.raise_for_status()
94
95 if response.encoding is None:
96 response.encoding = 'utf-8'
97
98 return response.json()
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

252@property
253def json(self):
254 """Get response body as json-decoded."""
255 return self.response.json()
24def request_json(self, url, return_code=200):
25 """
26 Request JSON content and expected return code.
27 :param url: URL to request
28 :param return_code: expected return code or raise error
29 :return: JSON, SuccessState
30 """
31 try:
32 header = {
33 'User-Agent': str(self.ua.google)
34 }
35 if not validators.url(url):
36 self.print_yellow(" [!] Invalid URL Requested: %s" % (str(url)))
37 return {}, False
38 r = requests.get(url, headers=header)
39 if r.status_code != return_code:
40 self.print_yellow(" [!] Request returned invalid status code: (CODE): %s (EXPECTED): %s" %
41 (str(r.status_code), str(return_code)))
42 return {}, False
43 return r.content, True
44 except requests.ConnectTimeout as e:
45 self.print_red(" [!] Request ConnectionTimeout: (URL): %s (ERROR): %s" % (str(url), str(e)))
46 return {}, False
47 except requests.TooManyRedirects:
48 self.print_red(" [!] Request TooManyRedirects: (URL): %s (ERROR): %s" % (str(url), str(e)))
49 return {}, False
50 except requests.HTTPError:
51 self.print_red(" [!] Request TooManyRedirects: (URL): %s (ERROR): %s" % (str(url), str(e)))
52 return {}, False
53 except ConnectionError as e:
54 self.print_red(" [!] Request ConnectionError: (URL): %s (ERROR): %s" %(str(url), str(e)))
55 return {}, False
56 except Exception as e:
57 self.print_red(" [!] Request Unknown Error: (URL): %s (ERROR): %s" % (str(url), str(e)))
58 return {}, False
37def response_json(response):
38 return json.loads(response.data.decode())
159def get_json(response):
160 """Get JSON from response."""
161 return json.loads(response.get_data(as_text=True))
11def json(self):
12 return json.loads(self.text)
57def json_response(*, status_=200, list_=None, headers_=None, **data):
58 return Response(
59 body=json.dumps(data if list_ is None else list_).encode(),
60 status=status_,
61 content_type=JSON_CONTENT_TYPE,
62 headers=headers_,
63 )
169def get_json(self):
170 if self.json is not None:
171 return self.json
172 return {}
204def json_response(**data):
205 return json.dumps(data)
30async def json(self):
31 return self.response

Related snippets