【问题标题】:Recreate curl command that sends JSON as multipart/form-data using Python-Requests使用 Python-Requests 重新创建将 JSON 作为 multipart/form-data 发送的 curl 命令
【发布时间】:2016-04-10 19:09:23
【问题描述】:

我正在尝试创建以下 curl POST 命令的 Python-Requests 版本(完美运行并提供预期响应):

curl -F 'json={"method":"update_video","params":{"video":{"id":"129263001","itemState":"INACTIVE"},"token":"jCoXH5OAMYQtXm1sg62KAF3ysG90YLagDAdlhg.."}}' https://api.somewebservice.com/services/post

使用:

curl -v -F 'json={"method":"update_video","params":{"video":{"id":"582984001","itemState":"INACTIVE"},"token":"jCoXH5OAMYQtXm1sg62KAF3ysG90YLagEECDAdlhg.."}}' https://api.somewebservice.com/services/post

我得到以下信息(仅包括所有 TLS 握手后的输出、服务器证书数据等):

....

> POST /services/post HTTP/1.1
> User-Agent: curl/7.41.0
> Host: api.somewebservice.com
> Accept: */*
> Content-Length: 294
> Expect: 100-continue
> Content-Type: multipart/form-data; boundary=------------------------871a9aa84d3c0de2
> 
< HTTP/1.1 100 Continue
< HTTP/1.1 200 OK
< Content-Type: application/json;charset=UTF-8
< Content-Length: 1228
< Date: Sun, 10 Apr 2016 07:04:00 GMT
< Server: somewebservice

鉴于上述 cURL 命令运行良好,并且此处的输出在详细模式下运行,我是否正确假设我需要做的是采用多部分/表单方法,该方法以表单形式发送 JSON 对象,如果我正在尝试使用 Python-Requests 重新创建它?

到目前为止,我已经尝试过:

import requests
import json

def deactivate_request():
    url = "https://api.somewebservice.com/services/post"
    headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
    payload = {"method":"update_video","params":{"video":{"id":"12926301","itemState":"INACTIVE"},"token":"jCoXH5OKAF3ysG90YLagEECTP16uOUSg_fEGDAdlhg.."}}
    r = requests.post(url, json=payload, headers=headers)
    print(r.text)

我也尝试过不同的变体,例如:

    r = requests.post(url, data=json.dumps(payload), headers=headers)

或者没有标题,像这样:

    r = requests.post(url, data=json.dumps(payload))

或者这个:

    r = requests.post(url, json=payload)

似乎没有任何效果,我只是不断收到相同的错误消息:

{"error": {"name":"MissingJSONError","message":"Could not find JSON-RPC.","code":211}, "result": null, "id": null}

针对该“211”错误的此 Web 服务的文档指出:

我们为 json 参数(对于非多部分帖子)或多部分帖子的第一部分获得了一个空字符串。

在使用 Requests 模块重新创建此 cURL 请求方面,我做错了什么?我认为我可以将有效负载对象作为表单编码数据发送,看起来这就是 cURL 命令对 -F 参数所做的事情。

【问题讨论】:

    标签: python json curl python-requests multipartform-data


    【解决方案1】:

    显然这个 curl 命令可以用以下命令重新创建:

    import requests
    
        def deactivate_request():
            url = "https://api.somewebservice.com/services/post"
            print url
            #headers = {"Authorization": "Bearer " + token, "Content-Type": "application/json"}
            headers = {'Content-Type': 'application/json'}
    
            print(headers)
    
            payload = 'json={"method":"update_video", "params":{"video":{"id":"620001", "itemState":"INACTIVE"}, "token":"jCoXH5OAMYQtXm1sg62KAF3yECTP16uOUSg_fEGDAdlhg.."}}'
            # using params instead of data because we are making this POST request by
            # constructing query string URL with key/value pairs in it.
    
            r = requests.post(url, params=payload, headers=headers)
    

    不是很明显,因为 curl 命令在其标题中使用 'multipart/form-data' 而在上面我们只是使用 'application/json'

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-14
      • 2018-04-08
      • 2015-09-19
      • 1970-01-01
      相关资源
      最近更新 更多