Home > other >  Send POST request from python to laravel
Send POST request from python to laravel

Time:10-25

I need to send a dictionary, with a POST request, created in Python to a web pages made in Laravel. I saw in Internet that I need to import a request library but I don't understood how does it works.

My python script is this:

 mac_dict = {}

def readFile():
    with open("/home/pi/Desktop/Progetti SIoTD/device.txt", "r") as file:
        for i in file:
            line, *lines = i.split()
            if line in mac_dict:
                mac_dict[line]  = lines
            else:
                mac_dict[line] = lines
    print(mac_dict)
    print("\n")
    return mac_dict

'''
def get_all_values(nested_dictionary):
    for key, value in nested_dictionary.items():
        if type(value) is dict:
            get_all_values(value)
        else:
            print(key, ":", value)
    print("\n")
'''
def getValues(dict, mac):
    s = 0
    rssi_val = []
    for key in dict:
        if key == mac:
            k = dict.get(mac)
            for i in range(len(k)):
                if i % 2 == 0:
                    rssi = k[i]
                    rssi = int(rssi)
                    print(rssi)
                    rssi_val.append(rssi)
                else:
                    k_v = k[i]
                    print(k_v)

    for i in range(len(rssi_val)):
        s  = rssi_val[i]

    average = s / len(rssi_val)
    return average

readFile()
#get_all_values(mac_dict)
getValues(mac_dict, 'C4:A5:DF:24:05:7E')

How can I made it? Thanks in advance

CodePudding user response:

If you have websites written in 2 different languages, the most robust method to establish a relationship between them is the API method. You can accept post request by typing api in the system written in Laravel. Then you can easily send the text or file by python in this post request to laravel api url.

CodePudding user response:

send API and In Laravel use

`public function __construct(){
 $data = "//API Here//";
 $this->data = $data;
}`

`public function store(){
      // rest of the code here you can find any where
 }
  `
  • Related