working with REST api internet resources with python and python requests

Posted on Feb. 18, 2019
python
python-requests
1731

In this article we are going to see how to use python packages/modules to request REST api mostly web services publicly available.

If you are requesting to an URL it should be the one of the type below from CRUD operations.

  • POST ( Create )
  • GET ( Read)
  • PUT ( Update)
  • DELETE ( Delete)

Lets say you have the API access to your resources through web services and you want to automate some tasks then you can use python requests package.

To perform above actions using python we can use urllib or requests.

By Using urllib we can fetch the internet resources like below example.

import urllib.request

req = urllib.request.Request('http://www.voidspace.org.uk')
response = urllib.request.urlopen(req)
the_page = response.read()

If you haven't tried to get internet resources previously using python or you don't know about urllib i recommend to go with requests package.

Python requests package

Python requests package allows us to send request to any URL in python scripts and to fetch the response. Python requests package is not available by default and you should install it before calling it in your code. Install the requests package like below.

pip install requests

It will install requests package in your python environment. Now it is very easy to use in your scripts.

Requests package follows the same CRUD methods defined above of this article. If you want to GET request you can use just requests.get method likewise same for all requests.post, requests.put, requests.delete .

For example I am using httpbin.org as. my API provider which is publicly available.

To get any response we should pass some arguments to methods of requests package like url, params, data, auth etc. You can understand these parameters with below samples.

requests.get

import requests

req = requests.get("https://httpbin.org/get")
print req.json()

For simple get request we can pass just url and if you want to add any url parameter You can simply add below line.

req = requests.get('https://httpbin.org/get', params={"arg1": "val1", "arg2": "val2"})

Then url will become https://httpbin.org/get?arg1=val1&arg2=val2.

You can try with this open API as like you want and get practise.

requests.post

If we have an API endpoint with POST method we can use requests.post to request for that resource. We can also send POST data to that URI like below.

import requests

url = "https://httpbin.org/post"
req = requests.post(url, data={
    "name": "kishore",
    "skill": "python"
})
out = req.json()

print out

If you observe the output you can see how our request with POST method sent to server. You can also send files in data dictionary itself by opening the file and adding file content as another parameter in data.

If you want to send your credentials to login/authenticated some URL, you can pass auth like below.

r = requests.post("http://my_api.url.com/login", auth=("username", "password"))
print r.status_code

 

 

By using this requests package we can request any URL with the methods as we want. So remaining methods also same by using requests.put and requests.delete methods.

 

Please find the below reference for further information and comment if you have doubts/suggestions.

 

https://reqres.in/api/users

https://koderplace.com/search/?tags=python-requests

 

https://pypi.org/project/requests/

https://docs.python.org/3.1/howto/urllib2.html

 




0 comments

Please log in to leave a comment.