Ticket #2942 (closed defect: wontfix)
API POST barfs on interesting Content-Type headers
Reported by: | dread | Owned by: | dread |
---|---|---|---|
Priority: | minor | Milestone: | |
Component: | ckan | Keywords: | |
Cc: | Repository: | ckan | |
Theme: | none |
Description
When POSTing to the API, if specified, the 'Content-Type' header must be blank or 'application/x-www-form-urlencoded'. Otherwise we get an error like: "Bad request - JSON Error: Could not extract request body data: Bad content type: \'; charset=utf-8\'""
The problem is that this is a very reasonable header to send. Indeed requests 0.14 sends this particular header.
This affects all versions of CKAN.
This is due to webob/requests.py:1248 being pretty basic.
Change History
comment:1 follow-up: ↓ 2 Changed 19 months ago by dread
- Status changed from new to closed
- Resolution set to wontfix
comment:2 in reply to: ↑ 1 Changed 19 months ago by dread
>>> requests.post('http://datahub.io/api/action/package_list', data='{}', headers={'content-type': 'application/x-www-form-urlencoded'}).content
Or even better:
>>> requests.post('http://datahub.io/api/action/package_list', data='{}', headers={'content-type': 'application/json'}).content
Note: See
TracTickets for help on using
tickets.
Unfortunately with a few hours hacking I couldn't find a way to get this version of WebOb? to cope with this header.
The line I think it goes wrong is ctype = env.get('CONTENT_TYPE', 'application/x-www-form-urlencoded') in request.py, and the commented line above looks right, but wipes the request data for some reason. I tried intercepting the bad header and deleting it, but I couldn't do that before WebOb? processed it, and couldn't persuade WebOb? to reprocess it once the header was edited.
I did write a failing test though if that is useful to someone in the future:
There is a clear error to the user:
The advice to users of requests would be to ensure they specify the content-type as follows: