I have this curl:
curl -v "http://some.url" \
-X PUT \
-H "X-API-Key: API_KEY" \
-H 'Accept: application/json' \
-H 'Content-Type: multipart/form-data' \
-F "logo=#the_logo.png;type=image/png"
And I am trying to replicate this with Python.
What I have tried so far is:
import requests
with open("the_logo.png", "rb") as file_obj:
data = file_obj.read()
requests.put(
"http://some.url",
files={"logo": ("the_logo.png", data, "image/png")},
headers={
"X-API-Key": "API_KEY",
"Accept": "application/json",
"Content-Type": "multipart/form-data"}
But for some reason the curl above works, while the Python code does not and the server replies with a 422.
How can I make Python replicate what curl does?
After some reading, it appears that the trick is to NOT set Content-Type in the headers when using requests and the files parameter.
Related
I am trying to POST a multipart/base64 xml file to portal using the following in python. How can i run it in Python?
curl -X POST -H 'Accept: application/xml' -H 'Content-Type: multipart/related; boundary=<boundary_value_that_you_have>; type=text/xml; start=<cXML_Invoice_content_id>' --data-binary #<filename>.xml https://<customer_name>.host.com/cxml/invoices
You can use this website
I got this code. Could you try it ?
import requests
headers = {
'Accept': 'application/xml',
'Content-Type': 'multipart/related; boundary=<boundary_value_that_you_have>; type=text/xml; start=<cXML_Invoice_content_id>',
}
data = open('filename.xml', 'rb').read()
response = requests.post('https://<customer_name>.host.com/cxml/invoices', headers=headers, data=data)
I have API and I need to send a .csv file using cURL in python. I do not know how to write this command on python.
curl --location --request POST 'http://**************' \
--header 'Accept: application/json' \
--header 'API-AUTH-TOKEN: **************' \
--form 'list=#"/C:/Users/1288956/Downloads/ozon_search_query_test.csv"'
means I can't show it
R.J. Adriaansen gave me a good answer: curlconverter.com
import requests
headers = {
'Accept': 'application/json',
'API-AUTH-TOKEN': '**************',
}
files = {
'list': ('"/C:/Users/1288956/Downloads/ozon_search_query_test.csv"', open('"/C:/Users/1288956/Downloads/ozon_search_query_test.csv"', 'rb')),
}
response = requests.post('http:///**************', headers=headers, files=files)
The call I want to make is this:
curl -i \
-H 'Harvest-Account-ID: 3012210627'\
-H 'Authorization: Bearer 184740.pt.VgNF7lMe1YDTjTH4_nfhm8NiH(qMyRI9kFS4BBvztnLM9P0HNgQvAHnlglnTA9q0wlmtrpoEONHVaT7phZAaNw'\
-H 'User-Agent: Harvest API Example' \
"https://api.harvestapp.com/api/v2/time_entries.json"
but I am not quite sure of how to do this. Any help will be appreciated.
It does not authenticate, because you did not send the JWT Token in HTTP headers:
import requests
headers = {
"Harvest-Account-ID": "380637",
"Authorization": "Bearer 184740.pt.VgNF7mMe1YDTjTH4_nfhm8NiT5IMyRI9kFS4AAvztnLM9P0HNgQvAHnlglnTA9X0wlmtrpoEONHVaT7phZAaNw",
"User-Agent": "Harvest API Example"
}
print (requests.get("https://api.harvestapp.com/api/v2/time_entries.json", headers=headers)
Returns:
<Response [200]>
Could you please inform what would be Python requests equivalent of following curl code to upload file to knack? Specifically part after -F option. Thank you
curl -X POST "https://api.knack.com/v1/applications/YOUR-APP-ID/assets/file/upload" \
-H 'content-type: multipart/form-data' \
-H 'x-knack-rest-api-key: YOUR-API-KEY' \
-F "files=#/path/to/your/file.txt"
Use requests.post with files and headers. Your curl code is equivalent to:
url = "https://api.knack.com/v1/applications/YOUR-APP-ID/assets/file/upload"
files = {'files':open('/path/to/your/file.txt', 'rb')}
headers = {'x-knack-rest-api-key': 'YOUR-API-KEY'}
r = requests.post(url, headers=headers, files=files)
When using the files argument, requests creates the necessary headers automatically so you don't need to have "Content-Type" or "Content-Length" in headers.
Having way too much trouble making this cmd line curl statement work in python script...help! Attempting to use URLLIB.
curl -X POST "http://api.postmarkapp.com/email" \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
-H "X-Postmark-Server-Token: abcdef-1234-46cc-b2ab-38e3a208ab2b" \
-v \
-d "{From: 'sender#email.com', To: 'recipient#email.com', Subject: 'Postmark test', HtmlBody: 'Hello dear Postmark user.'}"
Ok so you should probably user urllib2 to submit the actual request but here is the code:
import urllib
import urllib2
url = "http://api.postmarkapp.com/email"
data = "{From: 'sender#email.com', To: 'recipient#email.com', Subject: 'Postmark test', HtmlBody: 'Hello dear Postmark user.'}"
headers = { "Accept" : "application/json",
"Conthent-Type": "application/json",
"X-Postmark-Server-Token": "abcdef-1234-46cc-b2ab-38e3a208ab2b"}
req = urllib2.Request(url, data, headers)
response = urllib2.urlopen(req)
the_page = response.read()
Check out: urllib2 the unwritten manual
I get a 401 unauthorized response so I guess it works :)