I have a problem when I try to send data to a web service. It seems that the $_POST[ ] variable in the php file doesn't receive the content of the body of the http.post request in Flutter.
In Flutter, I use the following code :
var url = 'https://www.xxx';
var headers = {
"Content-Type": "application/json",
};
var requestBody = json.encode(
{ 'pays': infoPays, 'societe': infoSociete, });
final http.Response response = await http.post(
url,
body: requestBody,
headers: headers);
print(headers);
print(requestBody);
print(response.statusCode);
and my php file (web service) is the following :
<?php
//Header
header('Content-type: application/json');
//Connexion à la DB
try {
$bdd = new PDO('xxx');
} catch (\Exception $e) {
die('Erreur : '.$e->getMessage());
}
echo $_POST["pays"];
//Lecture du POST
$pays = isset($_POST["pays"])? htmlspecialchars($_POST["pays"]): "";
$societe = isset($_POST["societe"])? htmlspecialchars($_POST["societe"]): "";
$datetime = date ('Y-m-d H:i:s');
//Insertion des données
$req = $bdd->prepare("INSERT INTO Informations (pays, societe, info_date) values (?,?,?)");
$execute = $req->execute(array($pays, $societe, $datetime));
if ($execute) {
$responseArray = array(
'status' => 'true',
'message' => 'All data have been successfully stored.'
);
} else {
$responseArray = array(
'status' => 'false',
'message' => 'Error happened when storing data.'
);
}
echo json_encode($responseArray);
?>
In my DB I see that there is a new record (including the time stamp) and I receive a status code 200 proving that it works as it should but the data that the php file is supposed to receive from the Flutter http request (the body, I assume) are empty. It is proven by the echo of the $_POST["pays"] variable that shows nothing.
It seems thus that the $_POST[ ] variable in the php file doesn't receive the content of the body of the http.post request in Flutter.
Could you help me, please? I don't see what's wrong in the code (I must confess I am not a professional developper).
Many thanks !
Bernard