I am looking for assistance to use the REST API Function app to get the token and use the token to make an API.
I am able to get it to work in a python script, but I need help to make it work with the REST API Function App.
I got stuck on how to pass the token value to the API get request.
The python script below does both the get token and use the token to make an API get request. I think this help give you a good idea what I am trying to accomplish.
I also included the incomplete script from the REST API Function which I need help.
***Start of REST API Call function.***
***This script only has the get token portion since I don't know how to pass the token value to the API call
import json
headers = {
'Content-Type': 'application/x-www-form-urlencoded'
}
params = {
'username': 'my_username',
'password': 'my_secret'
}
inputs.rest_api_method = 'POST'
inputs.rest_api_headers = json.dumps(headers)
inputs.rest_api_url = 'https://myserver/api/login'
inputs.rest_api_verify = False
inputs.rest_api_query_parameters = json.dumps(params)
***End of REST API CAll function script***
***Beginning of the Python script****
import requests
url = 'https://myserver/api/login'
params = {
'username': 'my_username',
'password': 'my_secret'
}
response = requests.post(url, params=params)
# Check if the request was successful
if response.status_code == 200:
token = response.text
headers = {
'Authorization': token,
'Content-Type': 'application/x-www-form-urlencoded'
}
host_url = 'https://myserver/api/hosts/ip/10.0.0.1'
host_response = requests.get(host_url, headers=headers)
print(host_response.text)
***End of the python script****
------------------------------
Ray Tam
------------------------------