HTTP Post请求以及上传文件和标头可在Postman中使用,但不适用于python

时间:2019-03-07 15:01:17

标签: python upload postman

如果我上载了邮递员发送的有效内容,则不会显示任何文件上载错误,并且我尝试通过添加新的自定义有效内容来对代码进行的任何更改都只会显示相同的错误。

@app.route('/Upload', methods=["POST"])
def upload(sql) :
name = sql["name"]
version = sql["version"]
url = "https://dod-orasenatdhubsblue02.blockchain.ocp.oraclecloud.com:443/console/admin/api/v1/chaincodes/upload"


with open('C:\\Users\\Ravi\\Documents\\chaincodes\\generic\\generic.go', "rb") as file:
    payload = file.read() 

    values = {
    'Content-Disposition' : 'form-data',
    'chaincodeId' : 'test30',
    'chaincodeVersion' : 'v1',
    'chaincodeIndexes' : '[]',
    'myfile' :{
        'value': 'generic.go',
        'options':  {
            'filename' : file ,
            'Content-Type' : False ,
            'type' : 'file'
        }
    }
    }


    headers = {
    'content-type': "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
    'content-Type': "application/octet-stream",
    'Authorization': "Basic YXBpLnVzZXI6UXdlcnR5MTIzNDU2QA==",
    }   

    response = requests.post(url,data=values,headers=headers)

    print(response.text)

错误跟踪:

127.0.0.1 - - [07/Mar/2019 20:21:41] "POST /blockchain/Upload HTTP/1.1" 500 -
* Detected change in 'C:\\Users\\Ravi\\Documents\\upload.py', reloading
* Restarting with stat
* Debugger is active!
* Debugger PIN: 903-210-066
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
{"respMesg":"no file uploaded"}

2 个答案:

答案 0 :(得分:0)

我不知道这是否是错误,但是您缺少网址末尾的语音标记。

url =“ https://dod-orasenatdhubsblue02.blockchain.ocp.oraclecloud.com:443/co

您可以看到该网址的开头有语音标记,但结尾没有。

答案 1 :(得分:0)

从不输入内容类型

import requests
from requests.auth import HTTPBasicAuth
url = "your_url_here"

files = {
    "myfile": open("file_path_here","rb")
}

payload = {
    "chaincodeId": "id",
    "chaincodeVersion": "version",
    "chaincodeIndexes": "[]"
}

auth = HTTPBasicAuth('username', 'pass')

res = requests.post(url, auth=auth, files=files, data=payload)

print(res.content)