Ticket #1312 (closed enhancement: invalid)
Particular characters in JSON cause exception creating package on API
Reported by: | florian.marienfeld@… | Owned by: | dread |
---|---|---|---|
Priority: | minor | Milestone: | ckan-sprint-2011-10-28 |
Component: | ckan | Keywords: | |
Cc: | Repository: | ckan | |
Theme: | none |
Description
From Florian:
I am having trouble with the characters "=&;". if they occur e.g. in the title of a package, I get a 400-Bad-Request when I try to register a package by POSTing to ckan/rest/api/package However, when I PUT the same package as an edit to ckan/rest/api/package/existing_package, it works and the title shows correctly on the front end and API.
Reproduced
(pyenv-ckan)dread@dread-laptop:~/hgroot/ckan$ curl -i http://localhost:5000/api/rest/package -d '{"name": "test3", "title": "Test &"}' -H "Authorization:tester" HTTP/1.0 400 Bad Request Server: PasteWSGIServer/0.5 Python/2.6.5 Date: Tue, 06 Sep 2011 14:09:31 GMT Pragma: no-cache Cache-Control: no-cache Content-Type: application/json;charset=utf-8 Content-Length: 87 "Bad request - JSON Error: Unterminated string starting at: line 1 column 27 (char 27)"
Change History
Note: See
TracTickets for help on using
tickets.
The default Content-Type is "application/x-www-form-urlencoded" (that is what curl sends, and what CKAN defaults to). Therefore these characters need url encoding ("=&;" -> "%3D%26%3B"). Here is an example of it working:
Alternatively you can set the content type to "application/json" and you don't need to URL encode. e.g.:
To help work out these problems with encoding in the future, I've improved the error message to show the decoded JSON string. cset:55273629a552 which is headed for CKAN 1.5 release.