'My HTTP Python Server doesn't communicate with Grafana button (from button panel plugin)

I'm trying to communicate a button (from button panel) in grafana dashboard with a HTTP server I wrote in Python. The Python server behaves as it should when I command a POST from the command line in local, but when I try to use the POST via button plugin for Grafana it doesn't work.

Below is the server I wrote in python

import http.server
import socketserver
import time

class MyHandler(http.server.BaseHTTPRequestHandler):

    def do_POST(self):
        print("POST received")
        length = int(self.headers['Content-length'])
        print(self.rfile.read(length))
        self.send_response(200)
        self.send_header("Content-type", "text/html")
        self.end_headers()
        self.wfile.write("Hello World".encode('utf-8'))
        
    def do_OPTIONS(self):
        self.send_response(200, "ok")
        self.send_header('Access-Control-Allow-Origin', '*')
        self.send_header('Access-Control-Allow-Methods', 'GET, OPTIONS, HEAD, POST')
        self.send_header("Access-Control-Allow-Headers", "X-Requested-With")
        self.send_header("Access-Control-Allow-Headers", "Content-Type")
        self.send_header("Access-Control-Allow-Headers", "Content-length")
        self.send_header("Access-Control-Allow-Headers", "Daniele")
        self.send_header("Access-Control-Allow-Headers", '*')
        self.send_header("Accept", "application/json")
        self.send_header("Accept-Encoding", "gzip")
        self.send_header("Accept-Encoding", "deflate")
        self.send_header("Accept-Encoding", "br")
        self.send_header("Accept-Language", "it-IT,it;q=0.9,en-US;q=0.8,en;q=0.7")
        self.send_header("Content-Length", '4')
        self.send_header("Host", "<ip_number>")
        self.send_header("Origin", "http://<ip_number>")
        self.send_header("Refer", "http://<ip_number>/")
        self.send_header("Sec-Ch-Ua", "\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"99\", \"Google Chrome\";v=\"99\"")
        self.send_header("Sec-Ch-Ua-Mobile", "?0")
        self.send_header("Sec-Ch-Ua-Platform", "\"Windows\"")
        self.send_header("Sec-Fetch-Dest", "empty")
        self.send_header("Sec-Fetch-Mode", "cors")
        self.send_header("Sec-Fetch-Site", "same-origin")
        self.send_header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.74 Safari/537.36")
        self.send_header("X-Amzn-Trace-Id", "Root=1-624177b0-66851da6208b88a977ebb15a")
        self.end_headers()
        print('End of options')

PORT = <port_number>
Handler = MyHandler#http.server.SimpleHTTPRequestHandler

with socketserver.TCPServer(("", PORT), Handler) as httpd:
    print("serving at port", PORT)
    try:
        httpd.serve_forever()
    except KeyboardInterrupt:
        pass
    httpd.server_close()
    print(time.asctime(), "Server Stops - %s:%s" % ("", PORT))

I am running it on a Linux server, if I send a POST command from my PC locally via the following command

r = requests.post('http://<IP>:<PORT>', 'hello')

The server will receive the POST request and print "hello", in this way

enter image description here

The problem arises when I want to send a POST command through the button in grafana, below the configuration used

enter image description here

When you click on the button to send the command in Grafana, the button is written "failed to fetch" and the result on the server is that only the OPTIONS command is called without ever being called the POST

enter image description here

I can't understand why the do_POST() method is not called on the server. Someone would be able to help me thanks



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source