简体   繁体   English

将cURL命令转换为python请求

[英]Converting a cURL command to a python request

I am new to cURL; 我是cURL的新手; I wanted to convert a curl command of this structure: 我想转换这种结构的curl命令:

curl -X POST "http://127.0.0.1:8881/models/faewrfaw/v1/predict" -H "Content-Type:multipart/form-data" -F "data={\"key\": \"Path\"};type=application/json" -F "Path=@C:\Users\rtam\Music\1 2 3\TEST123.jpg"

to a python request. 到python请求。 I used https://curl.trillworks.com/ to assist me and got this as the output: 我使用https://curl.trillworks.com/来帮助我,并将其作为输出:

import requests

headers = {
    'Content-Type': 'multipart/form-data',
}

files = {
    'data': (None, '{"key": "Path"};type'),
    'Path': ('C:\\Users\\rtam\\Music\\1 2 3\\TEST123.jpg', open('C:\\Users\\rtam\\Music\\1 2 3\\TEST123.jpg', 'rb')),
}

response = requests.post('http://127.0.0.1:8881/models/faewrfaw/v1/predict', headers=headers, files=files)

However, when testing the response in python I got a bad request/request the server did not understand. 但是,当在python中测试响应时,我收到了错误的请求/请求,服务器无法理解。 I noticed the trillworks website did not account for the type (application/json) in its formatting, does it need to be in the python script? 我注意到trillworks网站的格式未考虑类型(application / json),是否需要使用python脚本?

This answer might help you: How to send JSON as part of multipart POST-request 这个答案可能对您帮助: 如何将JSON作为多部分POST请求的一部分发送

In your case it would be 在你的情况下

files = {
    'Path': (
        'C:\\Users\\rtam\\Music\\1 2 3\\TEST123.jpg', 
        open('C:\\Users\\rtam\\Music\\1 2 3\\TEST123.jpg', 'rb'), 
        'application/octet-stream'
    )
    'data': (None, '{"key": "Path"}', 'application/json'),
}

response = requests.post(
    'http://127.0.0.1:8881/models/faewrfaw/v1/predict',
    files=files
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM