Reputation: 15
js code: $.ajax({ type : "GET", async : true, url : url, contentType: "text/html", dataType : "json", beforeSend : function(request) { request.setRequestHeader("X-Test-Token", "abc"); }, ... });
when I get the request headers with cherrypy.request.headers,
I can't see the "X-Test-Token" in it.
how I can get the custom header in cherrypy?
Upvotes: 0
Views: 1385
Reputation: 300
It seems like an issue with the jQuery code. I used a JSFiddle to send JS requests to RequestBin and it seems like the header wasn't sent as expected.
The RequestBin is here: http://requestb.in/s5edg2s5?inspect
You may have to use a new RequestBin to test that code, since they expire. Anyway the bin showed me that the X-Test-Token header was not being sent, rather it was sent as the content of the Access-Control-Request-Headers header, not as an actual header. Try it out for yourself and you'll see.
The correct way to set custom headers is described in this SO post. I don't think you are using the beforeSend callback correctly.
Upvotes: 0
Reputation: 5741
Are you completely sure that the JS is working right?
This python code:
from pprint import pformat
import cherrypy as cp
class Root:
@cp.expose
def default(self):
return pformat(cp.request.headers)
cp.quickstart(Root())
With this curl command:
curl -H "X-Test-Token: abc" http://localhost:8080
Does work.
Upvotes: 1