for any developer that might encounter this issue, you better use http.vlient in your plugin
for rest api calls with post json payload here is a working example:
PHP Code:
import http.client
import json
# post a json body to a url https://www.example.com/api/GetBookDetails in order to fetch books details
connection = None
post_domain = 'example.com'
post_url = '/api/GetBookDetails'
post_headers = {'Content-Type': 'application/json; charset=utf-8'}
post_body = {'Books': [{'BookID': 1234}]}
try:
# connect to the server within 5 seconds timeout period
connection = http.client.HTTPSConnection(post_domain, timeout=5)
# perform the POST request with the json payload
connection.request("POST", post_url, json.dumps(post_body), post_headers)
# get the response from the server
response = connection.getresponse()
# make sure the server returned 200, OK response (and not an error)
if response.getcode() == 200:
# read the response text and decode it as utf-8
response_text = response.read().decode('utf-8')
# parse the response as a json object
try:
json_book_details = json.loads(response_text)
# print the json object
print(json.dumps(json_book_details, indent=4))
except json.JSONDecodeError as e:
print('Error parsing json response: ', e)
except Exception as e:
print('General error occurred while fetching book details:', e)
finally:
# close the connection
if connection:
connection.close()
change the url parts (domain, url) to match your site configuration