how to fetch data from API using python - python

I have to get data from rest api using python. how to send headers to retrieve data from API. is there any module for requesting data from API.

Try requests it has two method get() and post()
Please try:
import requests
import json
res = requests.get('paste your link here')
response = json.loads(res.text)

Previous answers have covered the idea behind how to fetch data from an API using python. Requests library is a natural selection if you want to achieve this.
Documentation and ref: https://requests.readthedocs.io/en/master/
Installation: pip install requests or https://requests.readthedocs.io/en/master/user/install/#install
Coming to the last part - how to send headers to retrieve data from API?
You can pass headers as dictionary to the request.
url = 'https://api.github.com/some/endpoint'
headers = {'user-agent': 'my-app/0.0.1'}
response = requests.get(url, headers=headers)
Now you have the response object in response variable; now it's up to you what you want to achieve. e.g. If you want to see what is the response body as String;
print(response.text)

Yes python has requests lib to make a call to POST and GET methods
e.g.
import requests
url = 'web address'
params = {'key':'value'}
r = requests.get(url = url, params = params)
response = r.json()

Related

How to add headers and body to a python request

I created a GET request in Python for an API and I would like to add headers and body
import urllib2
import os
proxy = 'http://26:Do#proxy:8080'
os.environ['http_proxy'] = proxy
os.environ['https_proxy'] = proxy
os.environ['HTTP_PROXY'] = proxy
os.environ['HTTPS_PROXY'] = proxy
contents = urllib2.urlopen("https://xxxx/lista?zile=50 ").read()
I tried in Postman and I received a response and I would like to receive the same response in python. How can I add headers and body ?
Thanks in advance
You can use the urlopen function with a Request object:
https://docs.python.org/2/library/urllib2.html#urllib2.urlopen
This Request object can contain headers and body:
https://docs.python.org/2/library/urllib2.html#urllib2.Request
Example: https://docs.python.org/2/howto/urllib2.html#data
P.S: HTTP GET requests don't have a body. Maybe you meant POST or PUT?
the best way is to use the request library which is pretty simple to use. https://realpython.com/python-requests/
example:
import requests
headers = {'Content-Type': 'application/json'}
data_json = {"some_key": "some_value"}
response = requests.post("https://xxxx/lista?zile=50", headers=headers, json=data_json)

Getting the JSON response from a POST request Python

I'm currently posting to a sever like so:
req = urllib2.Request('http://xxx.xxx.xx.xx/upload/')
req.add_header('Content-Type', 'application/json')
response = urllib2.urlopen(req, json_string)
print(response.getcode())
I get a 200 code back however I want to read the JSON the server is sending back. How do I do this? (tying to avoid using the requests library)
I did not get code, because I did not have a url.
Try:
req = urllib2.Request('http://xxx.xxx.xx.xx/upload/')
req.add_header('Content-Type', 'application/json')
response = urllib2.urlopen(req, json_string)
print(response.read())
To get the actual json object from the response not just the json serialised string you need to parse the response with the json library
import json
req = urllib2.Request('http://xxx.xxx.xx.xx/upload/')
req.add_header('Content-Type', 'application/json')
response = urllib2.urlopen(req, json_string)
json_response = json.loads(response.read().decode('ascii'))
The encoding may also be utf-8 depending on what the server sends back yo you.
Alternatively you could use the requests library which I find much easier to interact with, you'll need to install it separately though with pip install requests
import requests, json
response = requests.post('http://xxx.xxx.xx.xx/upload', data={'data': json_string})
if response.ok:
response_json = response.json()
else:
print('Something went wrong, server sent code {}'.format(response.status_code))
requests library docs

Python Requests API call not working

I'm having an issue converting a working cURL call to an internal API to a python requests call.
Here's the working cURL call:
curl -k -H 'Authorization:Token token=12345' 'https://server.domain.com/api?query=query'
I then attempted to convert that call into a working python requests script here:
#!/usr/bin/env python
import requests
url = 'https://server.domain.com/api?query=query'
headers = {'Authorization': 'Token token=12345'}
r = requests.get(url, headers=headers, verify=False)
print r
I get a HTTP 401 or 500 error depending on how I change the headers variable around. What I do not understand is how my python request is any different then the cURL request. They are both being run from the same server, as the same user.
Any help would be appreciated
Hard to say without knowing your api, but you may have a redirect that curl is honoring that requests is not (or at least isn't send the headers on redirect).
Try using a session object to ensure all requests (and redirects) have your header.
#!/usr/bin/env python
import requests
url = 'https://server.domain.com/api?query=query'
headers = {'Authorization': 'Token token=12345'}
#start a session
s = requests.Session()
#add headers to session
s.headers.update(headers)
#use session to perform a GET request.
r = s.get(url)
print r
I figured it out, it turns out I had to specify the "accept" header value, the working script looks like this:
#!/usr/bin/env python
import requests
url = 'https://server.domain.com/api?query=query'
headers = {'Accept': 'application/app.app.v2+json', 'Authorization': 'Token token=12345'}
r = requests.get(url, headers=headers, verify=False)
print r.json()

bad request error 400 while using python requests.post function

I'm trying to make a simple post request via the requests library of Python and I get a bad request error (400) while my url is supposedly correct since I can use it to perform a get.
I'm very new in REST requests, I read many tutorials and documentation but I guess there are still things I don't get so my error could be basic. Maybe a lack of understanding on the type of url I'm supposed to send via POST. Here my code :
import requests
v_username = "username"
v_password = "password"
v_headers = {'content-type':'application/rdf+xml'}
url = 'https://my.url'
params = {'param': 'val_param'}
payload = {'data': 'my_data'}
r = requests.post(url, params = params, auth=(v_username, v_password), data=payload, headers=v_headers, verify=False)
print r
I used the example of the requests documentation.
I had a similar problem, i tried changing params to data or with json.dumps():
from json import dumps
r = requests.post(url, params=dumps(params), auth=(v_username, v_password), data=payload, headers=v_headers, verify=False)
or
r = requests.post(url, data=dumps(params), auth=(v_username, v_password), data=payload, headers=v_headers, verify=False)

BLS API data using cURL and requests library?

All, I'm trying to implement a curl request to get data from the BLS. Following their example here (they show the curl request), my code looks like this:
import requests
headers = {'Content-type': 'application/json'}
params = {"seriesid":["LEU0254555900", "APU0000701111"],"startyear":"2002", "endyear":"2012"}
p = requests.post('http://api.bls.gov/publicAPI/v1/timeseries/data/', params = params,headers = headers)
print p.url
print p.content
I'm getting the following (error) output:
http://api.bls.gov/publicAPI/v1/timeseries/data/?seriesid=LEU0254555900&seriesid=APU0000701111&endyear=2012&startyear=2002
{"status":"REQUEST_FAILED","responseTime":0,"message":["Sorry, an
internal error occurred. Please check your input parameters and try
your request again."],"Results":null}
Anyone had to deal with the BLS api and python?
Is the requests library the best for this?
You need to send the data as json, not pass it as a params dict. params sets the url parameters, which is not what you want, you need to pass it as data.
This should work:
import requests
import json
headers = {'Content-type': 'application/json'}
data = json.dumps({"seriesid":["LEU0254555900", "APU0000701111"],"startyear":"2002", "endyear":"2012"})
p = requests.post('http://api.bls.gov/publicAPI/v1/timeseries/data/', data=data, headers=headers)
print p.url
print p.content

Categories