Hashtopolis Forum
Having trouble with API test connection using python - Printable Version

+- Hashtopolis Forum (https://hashtopolis.org)
+-- Forum: Support (https://hashtopolis.org/forum-1.html)
+--- Forum: Problems (https://hashtopolis.org/forum-2.html)
+--- Thread: Having trouble with API test connection using python (/thread-315.html)



Having trouble with API test connection using python - vbabson - 07-31-2019

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.


RE: Having trouble with API test connection using python - s3in!c - 07-31-2019

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.


RE: Having trouble with API test connection using python - vbabson - 07-31-2019

(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.


RE: Having trouble with API test connection using python - s3in!c - 07-31-2019

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))



RE: Having trouble with API test connection using python - vbabson - 07-31-2019

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"}