curl command using python and xml - python

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>"

Related

Retrieve answer from a python url request

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 ?

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)

How to convert "curl -X POST -T " to Python

How do I convert
curl -X POST -T 'sample_data.json' -H "Content-Type: application/json" https://sample_url.com
to Python using requests?
Specifically, how do I provide "-T" parameter to the request?
I believe you could do something like this:
import requests
import json
requests.post('https://sample_url.com',
headers = {'Content-type': 'application/json'},
data = json.loads(open('sample_data.json').read())
}
You can check out the requests page for more details.
Or, to show a fully self-contained example without having to load the json from a file, you could do:
import requests
requests.post('https://httpbin.org/post', data = {'key':'value'})
<Response [200]>
Note, from the requests docs:
Using the json parameter in the request will change the Content-Type in the header to application/json.
So, you could just do the following instead:
r = requests.post(url, json=payload)
curl -X POST -T 'sample_data.json' \
-H 'Content-Type: application/json' \
https://sample_url.com/
is effectively equivalent to
curl -X POST \
-H 'Content-Type: application/json' \
-d "$(cat sample_data.json)" \
https://sample_url.com/sample_data.json
so using requests, it would be
with open('sample_data.json') as fh:
response = requests.post(
"https://sample_url.com/sample_data.json",
data=fh.read(),
header={'Content-Type': 'application/json'}
)
There are a couple of ways that you could do it. But the best way is to use the requests library. It's not part of the standard library (yet), but makes HTTP requests super straight-forward.
$ pip install requests
or
$ conda install requests
Then
import json
import requests
url = r"https://sample_url.com"
with open("sample_data.json", "r") as fh:
data = json.load(fh)
requests.post(url=url, data=data)
The best way is to use the Python Requests library - https://2.python-requests.org/en/master/. To be able to post a JSON payload in the message is
import Requests
import json
result = requests.post(URL,json=json.loads(open('sample_data.json').read()))
The requests library natively understands how to send your JSON data
There are some more examples of how to post messages with Form encoded payloads on this part of the page as well - https://2.python-requests.org/en/master/user/quickstart/#more-complicated-post-requests
To be able to do other verbs you just need to change the function for requests -
for a GET, requests.get(url)
for a PUT, requests.put(url,data=DATA) or requests.put(url,json=JSON)
for a DELETE, requests.delete(url)
and so on.

Converting curl command to Python request

The curl command that I have that works properly is -
curl -X GET -H "Authorization: Basic <base64userpass>" -H "Content-Type: application/json" "http://<host>/bamboo/rest/api/latest/result/<plankey>.json?expand=results.result&os_authType=basic"
In Python, this is what I currently have -
headers = {'Authorization': 'Basic <base64userpass>', 'Content-Type': 'application/json'}
datapoints = {'expand': 'results.result', 'os_authType': 'basic'}
url = "http://<host>/bamboo/rest/api/latest/result/<plankey>.json"
r = requests.get(url, headers=headers, data=datapoints)
The response I get when using the Python request is <Response [403]>, but when using curl I get back the expected data.
What am I missing here?
Thanks.
You should use the auth option of requests to do basic authentication.
There are more headers that the CURL command-line handle for you (and requests will not handle them unless you use the auth):
>>> from requests.auth import HTTPBasicAuth
>>> requests.get('https://api.github.com/user', auth=HTTPBasicAuth('user', 'pass'))
Or just use:
>>> requests.get('https://api.github.com/user', auth=('user', 'pass'))
(Change the URL and everything).
Also note that requests should get the params= (and not data=).

how to make post request in python

Here is the curl command:
curl -H "X-API-TOKEN: <API-TOKEN>" 'http://foo.com/foo/bar' --data #
let me explain what goes into data
POST /foo/bar
Input (request JSON body)
Name Type
title string
body string
So, based on this.. I figured:
curl -H "X-API-TOKEN: " 'http://foo.com/foo/bar' --data '{"title":"foobar","body": "This body has both "double" and 'single' quotes"}'
Unfortunately, I am not able to figure that out as well (like curl from cli)
Though I would like to use python to send this request.
How do i do this?
With the standard Python httplib and urllib libraries you can do
import httplib, urllib
headers = {'X-API-TOKEN': 'your_token_here'}
payload = "'title'='value1'&'name'='value2'"
conn = httplib.HTTPConnection("heise.de")
conn.request("POST", "", payload, headers)
response = conn.getresponse()
print response
or if you want to use the nice HTTP library called "Requests".
import requests
headers = {'X-API-TOKEN': 'your_token_here'}
payload = {'title': 'value1', 'name': 'value2'}
r = requests.post("http://foo.com/foo/bar", data=payload, headers=headers)

Categories