How to do a HTTP DELETE request with Requests library

jorrebor picture jorrebor · Apr 17, 2012 · Viewed 59.9k times · Source

I'm using the requests package for interacting with the toggl.com API.

I can perform GET and POST requests:

    payload = {'some':'data'}
    headers = {'content-type': 'application/json'}
    url = "https://www.toggl.com/api/v6/" + data_description + ".json"
    response = requests.post(url, data=json.dumps(payload), headers=headers,auth=HTTPBasicAuth(toggl_token, 'api_token'))

but i cant seem to find a way to perform a DELETE request. Is this possible?

Answer

Elvis D'Souza picture Elvis D'Souza · Apr 17, 2012

Use requests.delete instead of requests.post

payload = {'some':'data'}
headers = {'content-type': 'application/json'}
url = "https://www.toggl.com/api/v6/" + data_description + ".json"

response = requests.delete(
    url, 
    data=json.dumps(payload), 
    headers=headers,
    auth=HTTPBasicAuth(toggl_token, 'api_token')
)