Page 1 of 1
VPN Server JSON-RPC API Python Example
Posted: Sun Jul 28, 2019 1:29 pm
by odianosen
Hello,
Sort of an entry-level python programmer here.
I want to make use of the new supported JSON-RPC API for a python script I am working on, but unfortunately cannot seem to get my head around how to go about it.
The documentation has examples in TypeScript, C# and JavaScript but nothing on Python. Please can someone help me with some python examples, so I can take it up from there?
Kind regards
Re: VPN Server JSON-RPC API Python Example
Posted: Tue Apr 07, 2020 7:16 am
by nickdsl
Did you find solution?
Re: VPN Server JSON-RPC API Python Example
Posted: Tue Apr 07, 2020 7:42 pm
by nickdsl
I had similar issue.
I write python script.
My example (don't work):
Code: Select all
# working with json
import json
# working with requests
import requests
# urllib3
import urllib3
# for debugging
import pdb
# server url
SERVER_URL = "https://FQDN_OR_IP_OF_YOUR_SERVER:5555"
# api url
API_URL = SERVER_URL + "/api/"
# adminhub
ADMIN_HUB = "" # for admin login leave empty
# server password
ADMIN_PASS = "YOUR PASS"
# my headers
HEADERS = {"X-VPNADMIN-HUBNAME": ADMIN_HUB, "X-VPNADMIN-PASSWORD": ADMIN_PASS}
PARAMS = {"jsonrpc": "2.0", "id": "rpc_call_id", "method": ""}
# disable warnings
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
MY_PARAMS = PARAMS
MY_PARAMS['method'] = "Test"
#MY_PARAMS['method'] = "GetServerInfo"
MY_PARAMS['params'] = {"IntValue_u32": 0}
#MY_PARAMS['params'] = {}
#pdb.set_trace()
response = requests.post(API_URL, data=MY_PARAMS, headers=HEADERS, verify=False)
#pdb.set_trace()
print(response.text)
and when i got:
{
"error": {
"code": 38,
"message": "Error code 38: Parameter is invalid: JSON-RPC Parse Error"
},
"jsonrpc": "2.0",
"id": "0"
}
Mistake - data must be string, not dictionary.
I replaced line:
Code: Select all
response = requests.post(API_URL, data=MY_PARAMS, headers=HEADERS, verify=False)
to
Code: Select all
response = requests.post(API_URL, data=json.loads(MY_PARAMS), headers=HEADERS, verify=False)
and then i got
{
"result": {
"Int64Value_u64": 0,
"IntValue_u32": 0,
"StrValue_str": "0",
"UniStrValue_utf": ""
},
"jsonrpc": "2.0",
"id": "rpc_call_id"
}
You should try same thing.
Re: VPN Server JSON-RPC API Python Example
Posted: Sat Nov 21, 2020 4:36 pm
by JAXPAROW
You should try the following codes, they work for me infact i am trying to implement using Django.
Code: Select all
import requests
import json
username = " "
password = "password_here"
gateway = "vpnserver.com" #server address here
def get_server_info(**params):
payload = {
"jsonrpc": "2.0",
"id": "rpc_call_id",
"method": "GetServerInfo",
"params": {
"HubName_str": params.get("hubname", None),
"Port_u32": params.get("port",None),
"Enable_bool": params.get("enabled",None)
}
}
headers= {'content-type': 'application/json'}
response = requests.request("POST", url=gateway, headers=headers, data = json.dumps(payload),
verify = False, auth = (username,password))
data = response.json()
data = print (response.text)
return data
get_server_info()