'How to convert curl -F command into python code with requests?
I am developing with apis however I faced with a problem that I have never met.
curl -F "media=@IMAGE_NAME" 'xxxx url'
How do I convert it into python code with requests?
Solution 1:[1]
I solved this problem.
files = { 'media': open(image_name, 'rb') }
Then
requests.post(url, files=files)
see http://www.python-requests.org/en/latest/user/quickstart/#post-a-multipart-encoded-file
Solution 2:[2]
There's a great example of a POST request in the manual. I think yours specifically would be:
r = requests.post("xxx url", data={"media": "@IMAGE_NAME"})
Solution 3:[3]
As per your case :
def command_string_generator(method, token, port, account,container=None, obj=None, headers_in=None, curlhead=None):
url = 'xxxx url'
image = "media=@IMAGE_NAME"
command_string = 'curl -F %s %s ' % (image ,url)
print command_string
return command_string
you can use a function like this as per your needs with -F and change accordingly. Hope this might be helpful.Additional parameters are just for convienience.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | |
Solution 3 | janonymous |