0
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?

lovecat
  • 15
  • 3

2 Answers2

1

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.

cyraxjoe
  • 5,661
  • 3
  • 28
  • 42
  • dosen't work...I set: cherrypy.tools.auth = cherrypy.Tool('before_handler', check_auth), and print the cp.request.headers in method "check_auth", can't see the header. – lovecat Dec 26 '14 at 07:01
  • Can you explain a little more about that tool? Are you enabling the tool? `cherrypy.tools.auth.on`? – cyraxjoe Dec 26 '14 at 08:27
0

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.

Community
  • 1
  • 1
Isa Hassen
  • 300
  • 1
  • 9