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)
Related
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)
I have the following Curl request:
curl -v --location --request POST 'http://127.0.0.1:8080/abc' \--header 'Content-Type: application/json' \--data-raw '{data}'
I tried using pycurl and requests command.
Also tried to put headers but it was of no use.
My tried code:
requests = "curl -v --location"
a = "http://127.0.0.1:8080/abc"
headers = {'Content-Type': 'application/json'}
r = requests.post(url=a, headers= headers , params=data)
Is this works?
https://curl.trillworks.com/
import requests
headers = {
'Content-Type': 'application/json',
}
data = '{data}'
response = requests.post('http://127.0.0.1:8080/abc',
headers=headers,
data=data)
I'm trying to grab some data from a website using API, but I'm having trouble converting the example curl command to python requests.
example curl command
curl -X POST "some_url" \
-H "accept: application/json" \
-H "Authorization: <accesstoken>" \
-d #- <<BODY
{}
BODY
My python requests that didn't work
headers = {
'Authorization': "Bearer {0}".format(access_token)
}
response = requests.request('GET', "some_url",
headers=headers, allow_redirects=False)
I get error code 400, can anyone help me figure out what was wrong?
The equivalent requests code for your curl should be:
import requests
headers = {
'accept': 'application/json',
'Authorization': '<accesstoken>',
}
data = "{} "
response = requests.post('http://some_url', headers=headers, data=data)
You can use https://curl.trillworks.com/ to convert your actual curl invocation (note that it won't handle heredocs, as in your example).
If you see different behavior between curl and your python code, dump the HTTP requests and compare:
Python requests - print entire http request (raw)?
How can I see the request headers made by curl when sending a request to the server?
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=
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]>