【问题标题】:How to do a HTTP DELETE request with Requests library如何使用 Requests 库执行 HTTP DELETE 请求
【发布时间】:2012-04-17 12:57:30
【问题描述】:

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

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

【问题讨论】:

    标签: python django httprequest


    【解决方案1】:

    使用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”参数在删除请求中会做什么?我们可以使用“数据”参数进行条件删除吗!
    【解决方案2】:
    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-04-24
      • 2018-03-22
      • 1970-01-01
      • 2017-12-25
      • 2017-01-23
      • 1970-01-01
      相关资源
      最近更新 更多