2

I have been using this function to handle http requests with no problems:

def do_request(self, method, url, **kwargs):

    params = kwargs.get('params', None)
    headers = kwargs.get('headers', None)
    payload = kwargs.get('data', None)
    request_method = {'GET':requests.get, 'POST': requests.post, 'PUT': requests.put, 'DELETE': requests.delete}

    request_url = url

    req = request_method[method]

    try:
        res = req(request_url, headers=headers, params=params, data=json.dumps(payload))
    except (requests.exceptions.ConnectionError, requests.exceptions.RequestException) as e:
        data = {'has_error':True, 'error_message':e.message}
        return data
    try:
        data = res.json()


        data.update({'has_error':False, 'error_message':''})
    except ValueError as e:
        msg = "Cannot read response, %s" %(e.message)
        data = {'has_error':True, 'error_message':msg}

    if not res.ok:
        msg = "Response not ok"
        data.update({'has_error':True, 'error_message':msg})
    if res.status_code >= 400:
        msg = 'Error code: ' + str(res.status_code) + '\n' + data['errorCode']
        data.update({'has_error':True, 'error_message': msg})

    return data

When I have to do a DELETE request without body entity I have no problems but when I try to add one (when required by the server) I get an error message from the server telling that the body cannot be null as if no body has been sent. Any ideas why this might be happening? I'm using requests module and python 2.7.12. As far as I know data can be send in a DELETE request. Thanks!

user3192199
  • 31
  • 1
  • 5
  • What do you mean "body entity"? – pbuck Mar 10 '17 at 20:00
  • I meant entity body or data in other words, sorry about that – user3192199 Mar 10 '17 at 20:23
  • There are problems with some clients and some servers when DELETE includes entity body: http://stackoverflow.com/questions/299628/is-an-entity-body-allowed-for-an-http-delete-request, for example & lots of search results. Some servers (apparently) convert the DELETE into a POST, others simply perform the DELETE but drop the body. In your case, investigate if your server allows delete with body? – pbuck Mar 10 '17 at 21:25
  • Thanks pbuck, you were right! Apparently as you said, I went an investigate the server and they are having problems with the body of the DELETE request, the body is being dropped by the server, so they suggested a workaround by changing the DELETE to POST!!!!! Thanks! – user3192199 Mar 11 '17 at 14:12
  • Great news... I've created an "answer" for this question so users in the future won't need to search through the comments. – pbuck Mar 11 '17 at 17:44

2 Answers2

0

There are problems with some clients and some servers when DELETE includes entity body: Is an entity body allowed for an HTTP DELETE request? for example & lots of search results.

Some servers (apparently) convert the DELETE into a POST, others simply perform the DELETE but drop the body. In your case, you've investigated that indeed, the body of a DELETE is dropped by the server & it has been suggested that you change the DELETE to POST.

Community
  • 1
  • 1
pbuck
  • 4,291
  • 2
  • 24
  • 36
0

Mmm... I can send a DELETE with body with Postman and works OK. But I cant get the same result with Requests 2.17.3

This is a issue related to Requests

ignabe
  • 2,310
  • 1
  • 15
  • 24