Retrieve answer from a python url request - python

I want to convert the following curl instruction into python code:
curl -X POST --insecure -H "Content-Type:audio/wav" --data-binary 'audio_test.wav' 'http://localhost:8888/file?l=atc&ac=atc'
This curl command returns: "One two five"
So, after some reseaches I decided to use the requests library. I made the following code:
import requests
url = 'http://localhost:8888/file?l=atc&ac=atc'
with open('audio_test.wav','rb') as fichier:
payload = fichier.read()
head = {'Content-Type':'audio/wav', 'Accept-Charset': 'utf-8'}
req = requests.post(url, data=payload, headers=head)
print(req)
And it gaves me: <Response [200]>. So I assume that it works.
However I don't have any ideas of how to retrieve the result ("One two five" ) from this since the curl command did it automatically.
Any suggestions ?

Related

errors from api when using python requests module for file upload

when i make the following request:
curl -X POST http://localhost:8080/endpoint \
-F "file=filepath/example.zip" \
-H "Content-Type: multipart/form-data"
everything works fine.
However, when i try to do the same in python:
import requests
url = "http://localhost:8080/endpoint"
files = {"file": open("example.zip", "rb")}
headers = {"Content-Type": "multipart/form-data"}
r = requests.post(url, files=files, headers=headers)
the api gives me an error. are these two code snippets not equivalent? thanks
the issue was this:
multipart data POST using python requests: no multipart boundary was found
it seems as though setting
headers = {"Content-Type": "multipart/form-data"}
caused the issue. i removed it and it works

Convert this curl command to Python 3

How can I send this curl out using Python?
I did find similar requests, but cannot adapt my code to suite the query
I have also tried using pycurl, following this example but without luck.
$ curl -v -X GET -H "Accept: text/csv" -H "Authorization: Basic YW5kcmVhLmJvdHRpQHdzcC5jb206OWY5N2E5YTY2ZWU1MTMxZjdmNjk4MDcwZTFkODEwMjU0M2I0NTg1ZA==" "https://epc.opendatacommunities.org/api/v1/domestic/search"
Thanks
If you are using the Python Requests package the following code snippet should work:
import requests
headers = {
'Accept': 'text/csv',
'Authorization': 'Basic YW5kcmVhLmJvdHRpQHdzcC5jb206OWY5N2E5YTY2ZWU1MTMxZjdmNjk4MDcwZTFkODEwMjU0M2I0NTg1ZA==',
}
response = requests.get('https://epc.opendatacommunities.org/api/v1/domestic/search', headers=headers)
response.status_code # 200
response.text # "lmk-key,address1,address2,address3,postcode,buildi ..."
(Note: I used the https://curl.trillworks.com/ website to automatically make the conversion)

Trouble converting curl commands to python requests

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?

Python Curl request with access token not working

I am running below API query using curl I am getting json response.
curl -X POST -H 'Content-Type: application/json' -H 'X-Metabase-Session: sdfeffff-sdfdf-ffff-ffff-fffffffff' https://dash.test.sh/api/card/140/query/json
But when I am trying to run same query using python requests I am getting response
"API endpoint does not exist."
Below is the python code which I am trying
import requests
url = 'https://dash.test.sh/api/card/140/query/json'
header = {
'X-Metabase-Session': 'sdfeffff-sdfdf-ffff-ffff-fffffffff'
}
response = requests.get(url, headers=header)
print (response.text)
I expect actual json content
With curl, you are doing a POST request but in Python you are calling GET method of same API. And possibly, the GET method does not exists for the API.
So just change your Python code
From requests.get to requests.post
curl is using Post and you are using get, the issue is there:
response = requests.post(url, headers=header)

curl command using python and xml

I want to convert following command in python.
curl -H "Accept: application/xml" -H "Content-Type: application/xml" -X GET "https://api.globalgiving.org/api/public/projectservice/all/projects/ids?api_key=YOUR_API_KEY"
Try this:
import urllib
website = urllib.urlopen('http://www.google.com/')
siteData = website.read()
print siteData
It might help.
I am not the only one, who would recommend using great HTTP for Humans library called requests.
Install it:
$ pip install requests
And use as follows:
>>> import requests
>>> headers = {"Accept": "application/xml", "Content-Type": "application/xml"}
>>> url = "https://api.globalgiving.org/api/public/projectservice/all/projects/ids"
>>> req = requests.get(url, headers=headers, params={"api_key": "YOUR_API_KEY"})
>>> req.ok
False
>>> req.status_code
401
>>> req.text
u"<?xml version='1.0' encoding='UTF-8'?>\n<error_response>\n <error_code>401</error_code>\n <errors>\n <error>\n <error_message>api_key [YOUR_API_KEY] is not registered in the system.</error_message>\n </error>\n </errors>\n <status>Unauthorized</status>\n</error_response>"

Categories