如何使用Requests库发送HTTP DELETE请求

46

我正在使用requests包与 toggl.com API 进行交互。

我可以执行 GET 和 POST 请求:

    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'))

但我似乎找不到执行DELETE请求的方法。这可行吗?

2个回答

87

使用requests.delete代替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')
)

“data”参数在删除请求中有什么作用?我们能否使用“data”参数进行有条件的删除! - VasaraBharat

-1
import requests
url = 'url.example.ap'
headers = {
    'content-type': "multipart/form-data; boundary=---- ",
    'X-Auth-Token': ""anytoken"",
    'Cache-Control': "no-cache"
    }
r = requests.delete(url, headers=headers)
n = os.write(sys.stdout.fileno(), r.content)
print r.content == r.text

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接