如何使用请求库执行HTTP删除请求

2024-05-16 10:18:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用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'))

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


Tags: comtokenapijsonurldatagetsome
2条回答

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

相关问题 更多 >