Skip to content

Commit

Permalink
New: JSON_REQUEST_CONTENT_TYPES
Browse files Browse the repository at this point in the history
This setting defaults to ['application/json']. Useful for supporting
vendor-specific Content-Type headers. Responses will still carry
application/json.

Closes #1024.
  • Loading branch information
nicolaiarocci committed Jun 6, 2017
1 parent 9f4e509 commit c0450e4
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 3 deletions.
7 changes: 6 additions & 1 deletion CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@ Development

Version 0.8
~~~~~~~~~~~
- New: ``JSON_REQUEST_CONTENT_TYPES`` or supported JSON content types. Useful
when you need support for vendor-specific json types. Please note: responses
will still carry the standard ``application/json`` type. Defaults to
``['application/json']``. Closes #1024.
- New: ``ALLOW_CUSTOM_FIELDS_IN_GEOJSON`` allows custom fields in GeoJSON
(Martin Fous).
- New: Support for ``Feature`` and ``FeatureCollection`` GeoJSON objects.
Closes #769 (Martin Fous).
- Config options ``MONGO_AUTH_MECHANISM`` and
``MONGO_AUTH_MECHANISM_PROPERTIES`` added.
- Change: Support for Cerberus 1.0+. Closes #776 (Dominik Kellner, Brad P. Crochet).
- Change: Support for Cerberus 1.0+. Closes #776 (Dominik Kellner, Brad P.
Crochet).
- Change: Drop Flask-PyMongo dependency. Closes #855 (Artem Kolesnikov).
- Docs: code snippets are now Python 3 compatibile (Pahaz Blinov).
- Dev: after the latest update (May 4th) travis-ci would not run tests on
Expand Down
6 changes: 6 additions & 0 deletions docs/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,12 @@ uppercase.
``JSON_SORT_KEYS`` ``True`` to enable JSON key sorting, ``False``
otherwise. Defaults to ``False``.

``JSON_REQUEST_CONTENT_TYPES`` Supported JSON content types. Useful when
you need support for vendor-specific json
types. Please note: responses will still
carry the standard ``application/json``
type. Defaults to ``['application/json']``.

``VALIDATION_ERROR_STATUS`` The HTTP status code to use for validation errors.
Defaults to ``422``.

Expand Down
1 change: 1 addition & 0 deletions eve/default_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@
MULTIPART_FORM_FIELDS_AS_JSON = False
AUTO_COLLAPSE_MULTI_KEYS = False
AUTO_CREATE_LISTS = False
JSON_REQUEST_CONTENT_TYPES = ['application/json']

SCHEMA_ENDPOINT = None

Expand Down
4 changes: 2 additions & 2 deletions eve/methods/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ def payload():
"""
content_type = request.headers.get('Content-Type', '').split(';')[0]

if content_type == 'application/json':
return request.get_json()
if content_type in config.JSON_REQUEST_CONTENT_TYPES:
return request.get_json(force=True)
elif content_type == 'application/x-www-form-urlencoded':
return multidict_to_dict(request.form) if len(request.form) else \
abort(400, description='No form-urlencoded data supplied')
Expand Down
2 changes: 2 additions & 0 deletions eve/tests/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ def test_default_settings(self):
self.assertEqual(self.app.config['STANDARD_ERRORS'],
[400, 401, 404, 405, 406, 409, 410, 412, 422, 428])
self.assertEqual(self.app.config['UPSERT_ON_PUT'], True)
self.assertEqual(self.app.config['JSON_REQUEST_CONTENT_TYPES'],
['application/json'])

def test_settings_as_dict(self):
my_settings = {'API_VERSION': 'override!', 'DOMAIN': {'contacts': {}}}
Expand Down
13 changes: 13 additions & 0 deletions eve/tests/methods/post.py
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,19 @@ def test_post_location_header_hateoas_off(self):
self.assertTrue('Location' in r.headers)
self.assertTrue(self.known_resource_url in r.headers['Location'])

def test_post_custom_json_content_type(self):
data = {'ref': '1234567890123456789054321'}
r, status = self.post(self.known_resource_url, data,
content_type='application/csp-report')
self.assert400(status)

self.app.config['JSON_REQUEST_CONTENT_TYPES'] += \
['application/csp-report']
r, status = self.post(self.known_resource_url, data,
content_type='application/csp-report')
self.assert201(status)


def perform_post(self, data, valid_items=[0]):
r, status = self.post(self.known_resource_url, data=data)
self.assert201(status)
Expand Down

0 comments on commit c0450e4

Please sign in to comment.