Driving API interactions with Scripting
* REST API calls from Python
1. Install 'requests' module
python -m pip install requests
2. Go to python mode. Apply GET, POST, PUT and DELETE methods.
$ python
// Import the module that was imported earlier
>>> import requests
//Set the url
>>> url = 'http://jsonplaceholder.typicode.com/photos'
// GET command
>>> response = requests.get(url)
>>> print ( response.json() )
// The above command will return the json data for all the images
>>> jsonPayload = { 'albumId': 1, 'title': 'test', 'url': 'nothing.com', 'thumbnailUrl': 'nothing.com'}
>>> response = requests.post( url, json=jsonPayload' )
>>> response.json()
{'albumId': 1, 'title': 'test', 'url': 'nothing.com', 'thumbnailUrl': 'nothing.com', 'id': 5001}
>>> url = 'http://jsonplaceholder.typicode.com/photos/100'
>>> response = requests.put( url, json=jsonPayload )
>>> response.json()
{'albumId': 1, 'title': 'test', 'url': 'nothing.com', 'thumbnailUrl': 'nothing.com', 'id': 100}
>>> response = requests.delete(url)
>>> response.json()
{}
* Authentication
>>> import requests
>>> url = 'https://api.github.com/user'
>>> response = requests.get( url )
>>> response.json()
{'message': 'Requires authentication', 'documentation_url': 'https://developer.github.com/v3/users/#get-the-authenticated-user'}
// The above message is expected.
>>> response = requests.get( url, auth=('username', 'password') )
>>> response.json()
If any of the credentials are wrong, you will get something like this:
{'message': 'Bad credentials', 'documentation_url': 'https://developer.github.com/v3'}
We don't want to use the hardcoded username and/or password, so here we go:
>>> response = requests.get ( url, headers= {'Authorization': 'Bearer github_auth_code'} )
>>> response.json()
By this, we will get the same output as we get by using auth.
1. Install 'requests' module
python -m pip install requests
2. Go to python mode. Apply GET, POST, PUT and DELETE methods.
$ python
// Import the module that was imported earlier
>>> import requests
//Set the url
>>> url = 'http://jsonplaceholder.typicode.com/photos'
// GET command
>>> response = requests.get(url)
>>> print ( response.json() )
// The above command will return the json data for all the images
>>> jsonPayload = { 'albumId': 1, 'title': 'test', 'url': 'nothing.com', 'thumbnailUrl': 'nothing.com'}
>>> response = requests.post( url, json=jsonPayload' )
>>> response.json()
{'albumId': 1, 'title': 'test', 'url': 'nothing.com', 'thumbnailUrl': 'nothing.com', 'id': 5001}
>>> url = 'http://jsonplaceholder.typicode.com/photos/100'
>>> response = requests.put( url, json=jsonPayload )
>>> response.json()
{'albumId': 1, 'title': 'test', 'url': 'nothing.com', 'thumbnailUrl': 'nothing.com', 'id': 100}
>>> response = requests.delete(url)
>>> response.json()
{}
* Authentication
>>> import requests
>>> url = 'https://api.github.com/user'
>>> response = requests.get( url )
>>> response.json()
{'message': 'Requires authentication', 'documentation_url': 'https://developer.github.com/v3/users/#get-the-authenticated-user'}
// The above message is expected.
>>> response = requests.get( url, auth=('username', 'password') )
>>> response.json()
If any of the credentials are wrong, you will get something like this:
{'message': 'Bad credentials', 'documentation_url': 'https://developer.github.com/v3'}
We don't want to use the hardcoded username and/or password, so here we go:
>>> response = requests.get ( url, headers= {'Authorization': 'Bearer github_auth_code'} )
>>> response.json()
By this, we will get the same output as we get by using auth.
Comments
Post a Comment