def test_post(self): res = self.fetch('/echo', method='POST', headers={'Content-Type': 'application/json'}, body=json.dumps({'name': 'Steve'})) json_body = parse_json(res.body) assert json_body['name'] == 'Steve' res = self.fetch('/echo', method='POST', headers={'Content-Type': 'application/json'}, body=json.dumps({})) json_body = parse_json(res.body) assert 'name' not in json_body
def test_post(self): res = self.fetch( "/echo_json", method="POST", headers={"Content-Type": "application/json"}, body=json.dumps({"name": "Steve"}), ) json_body = parse_json(res.body) assert json_body["name"] == "Steve" res = self.fetch( "/echo_json", method="POST", headers={"Content-Type": "application/json"}, body=json.dumps({}), ) json_body = parse_json(res.body) assert "name" not in json_body
def test_get_path_param(self): res = self.fetch( "/echo_with_param/42?name=Steve", method="GET", headers={"Content-Type": "application/json"}, ) json_body = parse_json(res.body) assert json_body == {"name": "Steve"}
def parse_json_body(req): """Return the decoded JSON body from the request.""" content_type = req.headers.get('Content-Type') if content_type and core.is_json(content_type): try: return core.parse_json(req.body) except (TypeError, ValueError): pass return {}
def test_required_field_provided(self): res = self.fetch( '/echo', method='POST', headers={'Content-Type': 'application/json'}, body=json.dumps({'name': 'johnny'}), ) json_body = parse_json(res.body) assert json_body['name'] == 'johnny'
def test_required_field_provided(self): res = self.fetch( "/echo", method="POST", headers={"Content-Type": "application/json"}, body=json.dumps({"name": "johnny"}), ) json_body = parse_json(res.body) assert json_body["name"] == "johnny"
def _raw_load_json(self, req): """Return a json payload from the request for the core parser's load_json Checks the input mimetype and may return 'missing' if the mimetype is non-json, even if the request body is parseable as json.""" if not is_json_request(req): return core.missing return core.parse_json(req.body, encoding=req.charset)
def _raw_load_json(self, req): """Return a json payload from the request for the core parser's load_json Checks the input mimetype and may return 'missing' if the mimetype is non-json, even if the request body is parseable as json.""" if not is_json_request(req) or req.content_length in (None, 0): return core.missing body = req.stream.read(req.content_length) if body: return core.parse_json(body) return core.missing
def parse_json_body(req): if req.content_length in (None, 0): # Nothing to do return {} if is_json_request(req): body = req.stream.read() if body: try: return core.parse_json(body) except (TypeError, ValueError): pass return {}
def parse_json(self, req, name, field): """Pull a json value from the request.""" json_data = self._cache.get("json") if json_data is None: try: self._cache["json"] = json_data = core.parse_json(req.body) except json.JSONDecodeError as e: if e.doc == "": return core.missing else: raise return core.get_value(json_data, name, field, allow_many_nested=True)
def parse_json_body(req): if req.content_length in (None, 0): # Nothing to do return {} content_type = req.get_header('Content-Type') if content_type and 'application/json' in content_type: body = req.stream.read() if body: try: return core.parse_json(body) except (TypeError, ValueError): pass return {}
def parse_json_body(req): """Return the decoded JSON body from the request.""" content_type = req.headers.get("Content-Type") if content_type and core.is_json(content_type): try: return core.parse_json(req.body) except TypeError: pass except json.JSONDecodeError as e: if e.doc == "": return core.missing else: raise return {}
def _raw_load_json(self, req): """Return a json payload from the request for the core parser's load_json Checks the input mimetype and may return 'missing' if the mimetype is non-json, even if the request body is parseable as json.""" if not is_json_request(req): return core.missing # request.body may be a concurrent.Future on streaming requests # this would cause a TypeError if we try to parse it if isinstance(req.body, tornado.concurrent.Future): return core.missing return core.parse_json(req.body)
def parse_json_body(req): if req.content_length in (None, 0): # Nothing to do return {} if is_json_request(req): body = req.stream.read() if body: try: return core.parse_json(body) except json.JSONDecodeError as e: if e.doc == "": return core.missing else: raise return {}
def parse_json(self, req, name, field): """Pull a json value from the request.""" json_data = self._cache.get("json") if json_data is None: # We decode the json manually here instead of # using req.get_json() so that we can handle # JSONDecodeErrors consistently data = req.get_data(cache=True) try: self._cache["json"] = json_data = core.parse_json(data) except json.JSONDecodeError as e: if e.doc == "": return core.missing else: return self.handle_invalid_json_error(e, req) return core.get_value(json_data, name, field, allow_many_nested=True)
def parse_json(self, req, name, field): """Pull a json value from the request body.""" json_data = self._cache.get("json") if json_data is None: if not core.is_json(req.content_type): return core.missing try: self._cache["json"] = json_data = core.parse_json(req.body) except AttributeError: return core.missing except json.JSONDecodeError as e: if e.doc == "": return core.missing else: return self.handle_invalid_json_error(e, req) return core.get_value(json_data, name, field, allow_many_nested=True)
def parse_json(self, req, name, field): """Pull a json value from the request.""" if not (req.body and is_json_request(req)): return core.missing try: json_data = req.json except: data = req.body try: self._cache["json"] = json_data = core.parse_json(data) except wa_json.JSONDecodeError as e: if e.doc == "": return core.missing else: return self.handle_invalid_json_error(e, req) return core.get_value(json_data, name, field, allow_many_nested=True)
async def parse_json(self, req: quart.Request, name: str, field: Field): """Pull a json value from the request.""" data = self._cache.get("json") if data is None: if not is_json_request(req): return core.missing data = await req.get_data(False) try: self._cache["json"] = data = core.parse_json(data) except json.JSONDecodeError as err: if err.doc == "": return core.missing return self.handle_invalid_json_error(err, req) return core.get_value(data, name, field, allow_many_nested=True)
def _raw_load_json(self, req): """Return a json payload from the request for the core parser's load_json.""" if not core.is_json(req.content_type): return core.missing return core.parse_json(req.body)
def test_get_with_no_json_body(self): res = self.fetch("/echo", method="GET", headers={"Content-Type": "application/json"}) json_body = parse_json(res.body) assert "name" not in json_body
def test_get_path_param(self): res = self.fetch('/echo_with_param/42?name=Steve', method='GET', headers={'Content-Type': 'application/json'}) json_body = parse_json(res.body) assert json_body == {'name': 'Steve'}
def test_get_with_no_json_body(self): res = self.fetch('/echo', method='GET', headers={'Content-Type': 'application/json'}) json_body = parse_json(res.body) assert 'name' not in json_body
def _raw_load_json(self, req): """Return a json payload from the request for the core parser's load_json""" return core.parse_json(req.body)