1

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.

MNV
  • 117
  • 1
  • 8
  • there is nothing wrong in sending response body on DELETE request. Make sure you respond with response code 200. See http://stackoverflow.com/questions/6581285/is-a-response-body-allowed-for-a-http-delete-request – K.M. Apr 05 '17 at 21:49
  • @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 Apr 05 '17 at 21:56
  • You at least need to tell CherryPy that DELETE might have a body via `request.methods_with_bodies`. See example at http://docs.cherrypy.org/en/latest/config.html#cp-config-attaching-config-to-handlers – fumanchu Apr 06 '17 at 06:09
  • @fumanchu That works! If you make an answer I'll gladly accept it! – MNV Apr 06 '17 at 14:21

2 Answers2

1

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')}
Bastien B
  • 1,018
  • 8
  • 25
0

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

MNV
  • 117
  • 1
  • 8