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)
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 been trying to use requests to pull json data with client id and api key, but it won't work.
Here is the info I received from IT:
Request:
curl --location --request GET 'https://api.abc.com/def' \
--header 'clientid: testuser' \
--header 'Accept: application/json' \
--header 'apikey: testapikey1234' \
--header 'dc_session: UUID' \
--header 'dc_transaction_id: UUID'
I wrote the following code in python
import requests
import json
url = 'https://api.abc.com/def'
headers = {'Accept': 'application/json', 'clientid': 'testuser', 'apikey': 'testapikey1234', 'dc_session': 'UUID', 'dc_transaction_id': 'UUID'}
response = requests.get(url, verify = False, headers = headers)
print(response.status_code)
print(response.json())
Unfortunately the code returns 401 status code, with the error message:
{'error': {'type': 'INVALID_CLIENT_IDENTIFIER', 'message': 'Unauthorized. Missing or invalid clientID.'}}
Does anyone have any suggestions?
when i use curl to upload data to server everything is ok but when i use requests in python, api server not accepted the request.
Blockquote
curl:
curl -X POST https://tapi.bale.ai/botXXX/Senddocument \
-H 'content-type: multipart/form-data' \
-F chat_id=2040137555 \
-F document=#/var/tmp/2706021400.pdf \
-F caption=caption
requests:
import requests
headers = {
'content-type': 'multipart/form-data',
}
files = {
'chat_id': (None, '2040137555'),
'document': ('/var/tmp/2706021400.pdf', open('/var/tmp/2706021400.pdf', 'rb')),
'caption': (None, 'caption'),
}
response = requests.post('https://tapi.bale.ai/botXXX/Senddocument', headers=headers, files=files)
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.
Have a CURL request like that:
curl -X POST "https://page.com/login"
-H "accept: application/json" -H "Content-Type: application/json"
-d "{ \"username\": \"admin\", \"password\": \"pass\"}"
In Python I guess it should look like this:
import requests
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
data = {'username': 'admin', 'password': 'pass'}
response = requests.post('https://page.com/login', headers=headers, data=data)
response
After this it gives me [502] error for bad gateway. What am I doing wrong with my python query and how it should be modified?
Try using:
requests.post(..., json=data)
When you use data= requests will send it form encoded, to actually put json in the body you have to use json=