1
votes

I'm trying to receive a JSON via DELETE to a cherrypy server using:

@cherrypy.tools.json_in()
def delete(self):
    data = cherrypy.request.json

But I always get an error (Looks like cherrypy just throw the json away...). Is there a way to retrieve the json?

I googled around and saw some people saying sending a DELETE with body is wrong, but the specs don't say anything.

2
there is nothing wrong in sending response body on DELETE request. Make sure you respond with response code 200. See stackoverflow.com/questions/6581285/… - K.M.
@rubchick I'm trying to send a json to a cherrypy server, but when I try to access the json in the server, I get an error. - MNV
You at least need to tell CherryPy that DELETE might have a body via request.methods_with_bodies. See example at docs.cherrypy.org/en/latest/… - fumanchu
@fumanchu That works! If you make an answer I'll gladly accept it! - MNV

2 Answers

1
votes

Since the official documentation is not on cherrypy.org anymore, here is the new one.

By default request is parameter as follow:

request.methods_with_bodies = ('POST', 'PUT', 'PATCH')

TO allow a body for a DELETE http method you need to add DELETE to the above tuple.

If you use a class base dispatch:

import cherrypy

class SetOPages:

    _cp_config = {"request.methods_with_bodies": ('POST', 'PUT', 'PATCH', 'DELETE')}

    @cherrypy.expose
    def page(self):
        return "Hello, World!"

if you use a classic def dispatcher:

import cherrypy

@cherrypy.expose
def page(self):
    return "Hello, world!"
page._cp_config = {"request.methods_with_bodies": ('POST', 'PUT', 'PATCH', 'DELETE')}
0
votes

Ok, I blame my bad reading skills...

From the specs...

The DELETE method requests that the origin server delete the resource identified by the Request-URI

It still doesn't explicit say it shouldn't have a body, but...