python - 'requests' to send data from raspberry pi to database on the web doesn't work
Get the solution ↓↓↓For my Project I'm using a raspberry pi which, after processing values from the sensor with the function conv_aqi(), is supposed to send them on a database hosted on 000webhost so that everyone can see them. The code does not produce errors, but values are missing on the db. I'm trying to pass those values are json. This is the python script:
import requests
def sendDatatoServer():
url = 'https://*******.000webhostapp.com/api/insert.php'
data = {
'aqi_2_5' : 'conv_aqi(pmt_2_5)',
'aqi_10' : 'conv_aqi(pmt_10)',
}
print(data)
res = requests.post(url, data)
print(res.text)
sendDatatoServer()
where the insert.php on the 000webhost is the following:
<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
//Creating Array for JSON response
$response = array();
// Check if we got the field from the user
if (isset($_GET['AQI_2_5']) && isset($_GET['AQI_10'])) {
$AQI_2_5 = $_GET['AQI_2_5'];
$AQI_10 = $_GET['AQI_10'];
// Include data base connect class
$filepath = realpath (dirname(__FILE__));
require_once($filepath."/db_connect.php");
// Connecting to database
$db = new DB_CONNECT();
// Fire SQL query to insert data in sensorData
$result = mysql_query("INSERT INTO sensorData(AQI_2_5,AQI_10) VALUES('$AQI_2_5','$AQI_10')");
// Check for succesfull execution of query
if ($result) {
// successfully inserted
$response["success"] = 1;
$response["message"] = "Data from sensor successfully created.";
// Show JSON response
echo json_encode($response);
} else {
// Failed to insert data in database
$response["success"] = 0;
$response["message"] = "Something went wrong";
// Show JSON response
echo json_encode($response);
}
} else {
// If required parameter is missing
$response["success"] = 0;
$response["message"] = "Parameter(s) are missing. Please check the request";
// Show JSON response
echo json_encode($response);
}
?>
And what I get is "Parameters are missing. Please check the request". Can I please have a hand on understanding where I'm mistaken ? Thanks
Answer
Solution:
Your Python code is using HTTP POST to send data in the body but yourinsert.php
has been written to expect query string parameters from a GET request.
The simplest thing to do is to change your Python code to pass query string parameters by using GET and Request's params attribute:
res = requests.get(url, params=data)
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: foreach() argument must be of type array|object, null given
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.