You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It works all and well in a standard scenario, as shown in the documentation here. But if you are doing a Cross-Origin Resource Sharing, then the standard is to first run a OPTIONS request to get server's approval, then execute the POST request.
In my particular case, I'm using AngularJS's $http to run POST with some json body onto the route foo. AngularJS then first run a OPTIONS, which has no body, and the route return a 500, since request.json_body fails to decode empty body.
There are currently few workaround:
Make function foo only handles request_method='POST', add another function that has same route_name and handle request_method='OPTIONS'. The extra function can return an empty dict. Alternatively just extend function foo to handle cases with different request_method
On AngularJS, use customised header {'Content-Type': 'application/x-www-form-urlencoded'}. This will make $http skipping OPTIONS and go straight into POST
I feel the handling of OPTIONS request should be done by Pyramid automatically.
The text was updated successfully, but these errors were encountered:
The OPTIONS method is sent as part of a CORS preflighted request. Pyramid cannot handle it automatically, because it involves application policy (what headers to set on the response). This issue needs to be addressed by a custom decorator (defined in your app) or a higher-level framework (e.g., Cornice or rest-toolkit).
Say I have a route that expects some json POST body:
It works all and well in a standard scenario, as shown in the documentation here. But if you are doing a Cross-Origin Resource Sharing, then the standard is to first run a
OPTIONS
request to get server's approval, then execute thePOST
request.In my particular case, I'm using AngularJS's
$http
to runPOST
with some json body onto the routefoo
. AngularJS then first run aOPTIONS
, which has no body, and the route return a 500, sincerequest.json_body
fails to decode empty body.There are currently few workaround:
foo
only handlesrequest_method='POST'
, add another function that has sameroute_name
and handlerequest_method='OPTIONS'
. The extra function can return an emptydict
. Alternatively just extend functionfoo
to handle cases with differentrequest_method
{'Content-Type': 'application/x-www-form-urlencoded'}
. This will make$http
skippingOPTIONS
and go straight intoPOST
I feel the handling of
OPTIONS
request should be done by Pyramid automatically.The text was updated successfully, but these errors were encountered: