0

I'm fairly new to web development. I am currently trying to make a post request from my javascript website to my separately hosted python code (using cherrypy), and have received a "400 Bad Request" in the console of my web browser.

I think the issue may be with the cherrypy method I took from the link shown in the code, or with "data" in the javascript code. Cherrypy works fine for everything else in my Python code (none of the other methods involve receiving data from javascript, but rather python). I finally created a stackoverflow account after being stuck on this for a while. The exact error given in the browser console is: "Post [url] 400 (Bad Request)"

Any help would be greatly appreciated.

// From the Website (Post Request): 
    $.ajax({
        url:'relevanturl',
        type:"POST",
        // id, title, start_time, and end_time are strings, and userlist is an array of strings 
        data:{id:id, title:title, start_time:start_time, end_time:end_time, userlist:userlist},
        success:function()    {
        },                 
        error:function(jqXHR,textStatus,errorThrown
        {alert('Exception:'+errorThrown);}
    });
# The specific cherrypy method not working (I made it with help from
# this link that shows how to handle AJAX requests: 
# https://stackoverflow.com/questions/3743769/how-to-receive-json-in-a-post-request-in-cherrypy

    @cherrypy.expose
    def add_meeting(self, data=None):
        cl = cherrypy.request.headers['Content-Length']
        rawbody = cherrypy.request.body.read(int(cl))
        body = simplejson.loads(rawbody)

        # For now, I'm just trying to receive the data from the website.
        print(body)
2

1 Answer 1

1

The information from the link in the comments solved the problem. How to receive JSON in a POST request in CherryPy?

The solution was to convert the data into JSON, and follow the necessary $.ajax syntax for JSON. To receive the data with CherryPy, @cherrypy.tools.json_in() must be called along with the usual "expose," and all the other relevant Python code from the link.

Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.