简体   繁体   English

如何为以下 curl 请求编写 python 代码以发送发布请求并将图像上传为二进制文件

[英]How to write python code for the below curl request to send a post request and uploading an image as binary

curl --request POST -H "Content-Type: application/octet-stream" --data-binary "@/C:\\Users\\U6068366\\Downloads\\Koala.jpg"  https://c6y09pww43.execute-api.us-east-1.amazonaws.com/p

-- ——

App_Url = "https://p7a0km3l6k.execute-api.us-east-1.amazonaws.com/preprod/v1/images/trademark/metadata/providerPartition/{providerPartition}/providerPartitionId/{providerPartitionId}"
    # f = open('C://Users//UX016491//PycharmProjects//DSSApi//data1.json')
    # requests_json = json.loads(f.read())
    files = {'media' : open('C:\\Users\\UX016491\\Desktop\\images\\image123.jpg','rb') }
    response = requests.request("POST", App_Url, files = files, headers={"content-type": 'application/octet-stream'})
    print(response)

if __name__ == '__main__':
    test_createimage_data()

EDIT: I added url from python example because url in curl was incomplete.编辑:我从 python 示例中添加了 url,因为 curl 中的 url 不完整。 But it still need two values providerPartition and providerPartitionId但它仍然需要两个值providerPartitionproviderPartitionId


On https://curl.trillworks.com/ you can convert curl to python code.https://curl.trillworks.com/ 上,您可以将curl转换为 Python 代码。 And mostly it works.大多数情况下它都有效。

Here code from this page.这是来自此页面的代码。 But I can't test it.但我无法测试它。

import requests

# incompletet url from curl
#url = 'https://c6y09pww43.execute-api.us-east-1.amazonaws.com/p'

providerPartition = '??'
providerPartitionId = '??'

url = f'https://p7a0km3l6k.execute-api.us-east-1.amazonaws.com/preprod/v1/images/trademark/metadata/providerPartition/{providerPartition}/providerPartitionId/{providerPartitionId}'


headers = {
    'Content-Type': 'application/octet-stream',
}

data = open('C:\\Users\\U6068366\\Downloads\\Koala.jpg', 'rb').read()

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

print(response.text)

You can also test both with url https://httpbin.org/post and it will send back what it get from you.您还可以使用 url https://httpbin.org/post 进行测试,它会发回从您那里得到的信息。 And you can compare results from both requests.您可以比较两个请求的结果。 I tested curl and python code and I go the same information so they should give the same effect.我测试了curlpython code ,我使用了相同的信息,所以它们应该给出相同的效果。

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

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