Search code examples
javascriptpythonhttpcherrypyaxios

Input being ignored on POST request with axios to a cherrypy function


So I am not sure which side of the code is wrong, the javascript or the python.

Basically I have reactjs forms, which are then to be submitted to the server and dealt with through cherrypy functions. The requests themselves are allegedly going through, but they aren't receiving any of the data I am sending.

Here is an example of one of the requests and the python function:

 handleSubmit(e){
      var example = this.props.value;
      const request = axios.post(someUrl+'/pythonFunction', {example})
      .then(function(response){
      console.log('successfully posted', example);
      })

    }

Now here is the Python code of the function this is being done at:

@cherrypy.tools.allow(methods='POST')
@cherrypy.tools.json_out()
def pythonFunction(self, **kwargs):
    # does stuff with data received

So, the problem is nothing is being received on the python end of things. The parameters such as kwargs will always be empty no matter how much I double and triple check to make sure things are being sent off appropriately.

Any ideas as to where the problem actually is and how to fix it?

Edit: not sure if relevant, but the data I am trying to post does appear on the Request Payload


Solution

  • Here's how to read JSON formatted payload (submitted via any HTTP method, which includes body):

    @cherrypy.tools.allow(methods='POST')
    @cherrypy.tools.json_out()
    @cherrypy.tools.json_in()
    def pythonFunction(self, *args **kwargs):
        data = cherrypy.request.json
        return data