Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Having trouble with API test connection using python
#1
I have simple script:

Code:
# importing the requests library
import requests
 
# defining the api-endpoint 
API_ENDPOINT = 'http://REDACTED/api/server.php'
 
# data to be sent to api
data = {
'section':'test',
'request':'access',
#'accessKey': 'REDACTED' #Tried this with and w/o accessKey as docs say it is not required
}
 
# sending post request and saving response as response object
r = requests.post(url = API_ENDPOINT, data = data)
 
# extracting response text 
hc_url = r.text
print('The response is:%s'%hc_url)


It successfully executes but returns:

The response is:{"action":"INV","response":"ERROR","message":"Invalid query!"}

No connection information appears in the logs and I am not sure how I can troubleshoot this.
Reply
#2
I think you sent it to the wrong endpoint. https://<HOST>/api/server.php is for the agents, you need to use https://<HOST>/api/user.php.
Reply
#3
(07-31-2019, 07:03 PM)s3in!c Wrote: I think you sent it to the wrong endpoint. https://<HOST>/api/server.php is for the agents, you need to use https://<HOST>/api/user.php.
Thanks for the prompt response.
So I changed the URL to user.php and I misspoke in my first post.

My data is:

data = {
  "section":"test",
  "request":"connection"
}

Now my return status is:

The response is:{"section":"INV","request":"INV","response":"ERROR","message":"Invalid user api query!"}

Is there documentation somewhere that details the API and response codes?  I am confused why I sent section=test but I get a response of section=INV.
Reply
#4
Based on the error response (as even not the section is recognized) there must be something wrong in how you post or encode the data.
I'm not sure if you can specify the data directly like you do, if I'm right you need to encode the data into json string:

Code:
requests.post(url = API_ENDPOINT, data = json.dumps(data))
Reply
#5
Thank you kindly!!

Final code:

Code:
# importing the requests library
import requests
import json
 
# defining the api-endpoint 
API_ENDPOINT = "http://REDACTED/api/user.php"
 
data = {
  "section":"test",
  "request":"connection"
}
 
# sending post request and saving response as response object
r = requests.post(url = API_ENDPOINT, data = json.dumps(data))
 
# extracting response text 
hc_url = r.text
print('The response is:%s'%hc_url)


Response:

The response is:{"section":"test","request":"connection","response":"SUCCESS"}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)