api: give specific message on json calls missing content-type header (#29856)

This commit is contained in:
Frédéric Péters 2019-01-17 16:27:21 +01:00
parent 41c263b717
commit 009eac6053
2 changed files with 10 additions and 1 deletions

View File

@ -565,6 +565,9 @@ def test_formdef_submit(pub, local_user):
assert data_class.get(resp.json['data']['id']).user_id == str(local_user.id)
assert data_class.get(resp.json['data']['id']).tracking_code is None
resp = get_app(pub).post(url(), json.dumps({'data': {}}), status=400) # missing Content-Type: application/json header
assert resp.json['err_desc'] == 'expected JSON but missing appropriate content-type'
formdef.disabled = True
formdef.store()
resp = get_app(pub).post_json(url(), {'data': {}}, status=403)

View File

@ -125,7 +125,7 @@ class HTTPRequest(quixote.http_request.HTTPRequest):
length = int(self.environ.get('CONTENT_LENGTH') or '0')
payload = self.django_request.read(length)
try:
self.json = json_loads(payload)
self._json = json_loads(payload)
except ValueError, e:
raise RequestError('invalid json payload (%s)' % str(e))
# Make sure request.form doesn't contain unicode strings, converting
@ -135,6 +135,12 @@ class HTTPRequest(quixote.http_request.HTTPRequest):
for k, v in self.form.items())
self.parsed = True
@property
def json(self):
if not hasattr(self, '_json'):
raise RequestError('expected JSON but missing appropriate content-type')
return self._json
def is_json(self):
if self.is_json_marker:
return True