如何使用python中的请求发送文件和表单数据?
我使用python请求库进行以下调用:如何使用python中的请求发送文件和表单数据?,python,python-2.7,python-requests,ibm-cloud,ibm-blockchain,Python,Python 2.7,Python Requests,Ibm Cloud,Ibm Blockchain,我使用python请求库进行以下调用: response = requests.post( 'https://blockchain-starter.eu-gb.bluemix.net/api/v1/networks/<network id>/chaincode/install', headers={ 'accept': 'application/json', 'content-type': 'multipart/form
response = requests.post(
'https://blockchain-starter.eu-gb.bluemix.net/api/v1/networks/<network id>/chaincode/install',
headers={
'accept': 'application/json',
'content-type': 'multipart/form-data',
'authorization': 'Basic ' + b64encode(credential['key'] + ":" + credential['secret'])
},
data={
'chaincode_id': chaincode_id,
'chaincode_version': new_version,
'chaincode_type': chaincode_type,
'files': open('chaincode.zip', 'rb')
}
)
正如提问者所承认的,这个答案似乎解决了他们的问题 不要自己设置内容类型标题,将其留给任务生成
有帮助吗?是的!不是第一个答案,而是第二个答案。对任何感兴趣的人来说,解决方案都是删除
内容类型
标题并将文件移动到它自己的部分。
response = requests.post(
https://blockchain-starter.eu-gb.bluemix.net/api/v1/networks/<network id>/chaincode/install,
headers={
'accept': 'application/json',
'authorization': 'Basic ' + b64encode(credential['key'] + ":" + credential['secret'])
},
data={
'chaincode_id': chaincode_id,
'chaincode_version': new_version,
'chaincode_type': chaincode_language
},
files={
'file': open('chaincode_id.zip', 'rb')
}
)
def send_request():
payload = {"param_1": "value_1", "param_2": "value_2"}
files = {
'json': (None, json.dumps(payload), 'application/json'),
'file': (os.path.basename(file), open(file, 'rb'), 'application/octet-stream')
}
r = requests.post(url, files=files)
print(r.content)