[ckan-dev] CORS problem

Eric Lemoine eric.lemoine at gmail.com
Wed Jun 29 19:26:26 UTC 2011


On Wednesday, June 29, 2011, David Read <david.read at okfn.org> wrote:
> Will,
>
> Do you have any idea about this header issue?
> http://trac.ckan.org/ticket/1206
>
> BTW, I added 'Content-Type' to the allowable headers in the apache
> config on us7/test.ckan.net.
>
> Andrea Fiore is here in Berlin working on ckanjs and could use some
> help on this today, if you're around.

Hi

I'm new to this list and CKAN. I don't know the CKAN code at all at
this point, but knowing Pylons I might be able to bring some
information related to your questions.

I'm copying the ticket description below, and answering below it.

Replying to [ticket:1206 dread]:
> Compare these two requests to create a package:
> {{{
> curl http://test.ckan.net/api/rest/package -d '{name:"test"}' -H 'Content-Type: application/json' -H 'X-CKAN-API-KEY: tester'
> curl http://test.ckan.net/api/rest/package -d '{name:"test"}' -H 'X-CKAN-API-KEY: tester'
> }}}
> The second one gets the payload through (ckan log):
> {{{
> Retrieving request params: UnicodeMultiDict([('{name:"test"}', u'')])
> }}}
> But the first one causes a ServerError because the payload (name:"test") doesn't make it to request.POST or request.params:
> {{{
> Retrieving request params: UnicodeMultiDict([])
> }}}
> The only difference is the "ContentType: application/json" header, which seems a reasonable thing to include. Javascript lib backbone.js (for example) inserts this automatically.
>
> So why does this header cause the payload to not get through to the request object?

When no Content-Type header is specified on the command line curl uses
application/x-www-form-urlencoded for POST requests.

POST requests with Content-Type set to
application/x-www-form-urlencoded are supposed to contain key/value
pairs in their bodies. And Pylons makes these key value pairs
available in request.params.

So for your first request request.params is
UnicodeMultiDict([('{name:"test"}', u'')]), because the request
contains one key/value pair, whose key is {name:"test"} and whose
value is the empty string.

If you set Content-Type to application/json Pylons (correctly)
considers that you don't send parameters (key/value pairs), which
explains why request.params is an empty UnicodeMultiDict. However the
content of the request should be available in request.body, which you
could parse with, for example, Pyhon's standard JSON module:

import json
obj = json.loads(request.body)

Also, I note that the JSON you're trying to send isn't valid. It
should be {"name":"test"} as opposed to {name:"test"}.


Hope this helps,

-- 
Eric




More information about the ckan-dev mailing list