View Single Post
Old 05-17-2024, 11:53 PM   #6
dandman
Enthusiast
dandman is clearly one to watchdandman is clearly one to watchdandman is clearly one to watchdandman is clearly one to watchdandman is clearly one to watchdandman is clearly one to watchdandman is clearly one to watchdandman is clearly one to watchdandman is clearly one to watchdandman is clearly one to watchdandman is clearly one to watch
 
Posts: 29
Karma: 10545
Join Date: May 2024
Device: none
SOLVED! post request with custom headers from within a metadata plugin

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_domaintimeout=5)
    
# perform the POST request with the json payload
    
connection.request("POST"post_urljson.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_detailsindent=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

Last edited by dandman; 05-17-2024 at 11:57 PM.
dandman is offline   Reply With Quote