1

I have problem to retrieve query strings in webapp2 delete http method. I can not use self.request.get('my_query_string') to get specific query string. How do you retrieve it from delete method?

In client, I have the the following javascript code to send http request:

function send_http(value1, value2) {
    $.ajax({
        type: 'DELETE',
        url: '/path/to/my/handler'
        data: {key1: value1, key2: value2}
    });
}

In GAE webapp2, My handler is like following:

class MyHandler(webapp2.RequestHandler):
    def post(self):
        logging.debug('key1: %s' % self.request.get('key1')) # This is OK
        logging.debug('key2: %s' % self.request.get('key2')) # This is OK

   def put(self):
        logging.debug('key1: %s' % self.request.get('key1')) # This is OK
        logging.debug('key2: %s' % self.request.get('key2')) # This is OK

   def delete(self):
        logging.debug('key1: %s' % self.request.get('key1')) # This is NG
        logging.debug('key2: %s' % self.request.get('key2')) # This is NG
        logging.debug('body: %s' % self.request.body) # key1=value1&key2=value2

If client side changes the http method to POST or PUT, self.request.get() works well. However, if client side use DELETE method, self.request.get() only return empty.

If I check self.request.body, I can see all the query strings. Is there any convenient method for retrieving specific query string from delete method instead of parsing body myself?

正宗白布鞋
  • 929
  • 6
  • 13
  • 1
    I don't think jquery is passing in the data as query parameters. If you try something like this: '/path/to/my/handler?param1=1' and then call self.request.get('param1') it will return 1 for delete. – Rob Curtis Jul 22 '13 at 19:22
  • Thanks Rob, as you said, if I append data to url directly, self.request.get() works well. It seems I misunderstood data field in [jQuery.ajax](http://api.jquery.com/jQuery.ajax/) document. – 正宗白布鞋 Jul 23 '13 at 00:13
  • 1
    Above test code is all running on development server, after deploying it to production GAE, I found that when sending delete request to production GAE server, there can not be any content in the request body (above client code did add data in the body not url), or will get a '400 Bad Request' error. So, appending data to url manually is currently what I can do only. Thanks again, Rob. – 正宗白布鞋 Jul 23 '13 at 15:02
  • I think it depends on your interpretation of the http spec for delete. This question seems to think it's allowed though: http://stackoverflow.com/questions/299628/is-an-entity-body-allowed-for-an-http-delete-request – Rob Curtis Jul 23 '13 at 15:15
  • Yes, different implementations interpret http spec for delete different. I found this old [issue](https://code.google.com/p/googleappengine/issues/detail?id=601) today, the error code is changing from 501 to 400. – 正宗白布鞋 Jul 23 '13 at 17:30

0 Answers0