HTTP removal using python request module

I would like to do an HTTP DELETE with the python request module that follows the API below;

https://thingspeak.com/docs/channels#create

DELETE https://api.thingspeak.com/channels/4/feeds api_key=XXXXXXXXXXXXXXXX 

I am using python v2.7 and the query module. My python code is as follows:

 def clear(channel_id): data = {} data['api_key'] = 'DUCYS8xufsV613VX' URL_delete = "http://api.thingspeak.com/channels/" + str(channel_id) + "/feeds" r = requests.delete(URL_delete, data) 

The code does not work because request.delete () can take only one parameter. What should the correct code look like?

+5
source share
2 answers

Do you want to

 import json mydata = {} mydata['api_key'] = "Jsa9i23jka" r = requests.delete(URL_delete, data=json.dumps(mydata)) 

You should use a named input, "data", and I assume that you really want to dump JSON, so you need to convert the dictionary "mydata" to a json string. You can use json.dumps () for this.

I don’t know the API that you use, but by its sound you really want to pass the URL parameter, not the data, for this you need:

 r = requests.delete(URL_delete, params=mydata) 

No need to convert mydata dict to json string.

+4
source

You can send data parameters as suggested by @Eugene, but conditional deletion of requests contains only a URL and nothing else. The reason is that a RESTful URL must uniquely identify a resource, thereby eliminating the need to provide additional parameters for deletion. On the other hand, if your APIKEY has something to do with authentication, it should be part of the headers instead of the request data, something like this.

 headers = {'APIKEY': 'xxx'} response = requests.delete(url, data=json.dumps(payload), headers=headers) 
+2
source

All Articles