Python请求 - 带有其他paramas的POST文件

时间:2017-12-11 16:13:23

标签: python file python-requests

所以我有这个代码发送API请求将文件上传到服务器,在搜索网络后我想出了一些类似于“有效负载”的内容。是我需要发送的附加参数和文件'是我要上传的文件的路径:

def sendAPIRequest2(self, env, object, method, params, files):
    apiPath = "/api/secure/jsonws"
    headers = {'content-type': 'multipart/form-data'}
    url = env + apiPath + object
    payload = {
        "method": method,
        "params": params,
        "jsonrpc": "2.0"
    }
    data = json.dumps(payload)
    myFile = {'file': open(files,'rb')}
    try:
        r = requests.post(url, files=myFile, data=data, headers=headers, 
                auth=HTTPBasicAuth(self.user, self.password), verify=False)
        print r.text
        resjson = json.loads(r.text)
    except:
        print "no Valid JSON has returned from"+object+" API"
        sys.exit(1)
    return resjson

我明白了:

ValueError: Data must not be a string.

我做错了什么?

0 个答案:

没有答案