Search code examples
headerrequestcherrypy

cherrypy get custom request header failed


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?


Solution

  • 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.