VPN Server JSON-RPC API Python Example

Post your questions about SoftEther VPN software here. Please answer questions if you can afford.
Post Reply
odianosen
Posts: 3
Joined: Sat Jun 22, 2019 6:20 am

VPN Server JSON-RPC API Python Example

Post by odianosen » Sun Jul 28, 2019 1:29 pm

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

nickdsl
Posts: 8
Joined: Wed Nov 02, 2016 11:59 am

Re: VPN Server JSON-RPC API Python Example

Post by nickdsl » Tue Apr 07, 2020 7:16 am

Did you find solution?

nickdsl
Posts: 8
Joined: Wed Nov 02, 2016 11:59 am

Re: VPN Server JSON-RPC API Python Example

Post by nickdsl » Tue Apr 07, 2020 7:42 pm

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.

Post Reply