简体   繁体   中英

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. But it still need two values providerPartition and providerPartitionId


On https://curl.trillworks.com/ you can convert curl to python code. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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