test_connection
Test API Connection
This function is to test the API connection to the server via api request itself.
Request Header
X-API-USER:=eenos_api, you should use the created api username
X-API-PASSWORD=eenos123, you should use the password of the api user
API URL = https://your-host-name:5555/api//test_connection/
Request URL
Post URL = /test_connection/
Full URL = https://your-host-name:5555/api/test_connection/
Example Curl Command
curl -X POST \
-H "X-API-USER: eenos_api" \
-H "X-API-PASSWORD: eenos123" \
https://your-host-name:5555/api/test_connection/
Output:
{"data":{"test":"Connection Success"},"info":"API Connection Success","status":200}
If you can see a similar result like below, your API logins are ok and working. If you got any other error message, then you are using the wrong api logins or there is an error in connecting from your IP.IP
Python Code
#!/usr/bin/python3
import requests
from pprint import pprint
# The API request url
request_url='/test_connection/'
# The server api base url
base_url='https://your-host-name:5555/api'
# Default base url
# base_url='https://your-host-name:5555/api'
# Version-specific base url
# base_url='https://your-host-name:5555/api/v1'
# The api url
api_url=base_url+request_url
# The header
headers = {
"X-API-USER": "eenos_api",
"X-API-PASSWORD": "eenos123",
}
try:
response = requests.post(api_url, headers=headers)
response.raise_for_status()
pprint(response.json())
except Exception as e:
pprint(str(e))
Python Code
PHP Code
<?php
// The API request urlURL
$request_url=request_url = '/test_connection/';
//*
Your|--------------------------------------------------------------------------
server| API baseBase urlURL Options
|--------------------------------------------------------------------------
| Default API:
| $base_url=base_url = 'https://your-host-name:5555/api';
|
| Versioned API (recommended):
| $base_url = 'https://your-host-name:5555/api/v1';
Default|--------------------------------------------------------------------------
base url*/
// Base URL
$base_url=base_url = 'https://your-host-name:5555/api';
// Version specific baseAPI urlURL (uncomment if needed)
// $base_url=base_url = 'https://your-host-name:5555/api/v1';
// The FULLFull API URL
$api_url=api_url = $base_url.base_url . $request_url;
//The dataInitialize you want to send via POST
$fields= [
'apiuser' => 'eenos_api',
'apiauth' => 'eenos123',
'user' => 'myclient',
'selfown' => 'on'
];
//url-ify the data for the POST
$fields_string = http_build_query($fields);
//open connectioncURL
$ch = curl_init()$api_url);
//set the url, number ofNo POST vars,payload POSTfor datatest_connection
curl_setopt($ch,CURLOPT_URL, $api_url);
curl_setopt($ch,CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
//So that curl_exec returns the contents of the cURL; rather than echoing it
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
//execute postAuthentication headers (new API format)
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'X-API-USER: eenos_api',
'X-API-PASSWORD: eenos123'
]);
// Execute request
$result = curl_exec($ch);
//Print theError jsonhandling
resultif (curl_errno($ch)) {
echo 'cURL Error: ' . curl_error($ch);
} else {
echo json_encode(json_decode($result), JSON_PRETTY_PRINT);
}
// Close connection
curl_close($ch);
?>