I am having a hard time trying to get the JSON
data fields, out of the post request sent from a mobile application.
On the client (mobile app) the JSON
data is easily encoded using the model class.
Future<http.Response> postRequest(CustomerData data) async {
var url = 'http://xxxxxxxxxxxx/testSendData.php';
var body = data.toJson();
var response = await http.post(url, headers: {"Content-Type": "application/json"}, body: body);
print("RESPONSE STATUS : ${response.statusCode}");
print("RESPONSE BODY : ${response.body}");
return response;
}
This part works well and generates a JSON
like:
{
"creation":"12/01/2019",
"status":"paid",
"price":0.0,
"items":[
{
"name":"Math books",
"amount":"2"
},
{
"name":"Skates adult",
"amount":"1"
},
{
"name":"Tools",
"amount":"8"
}
]
}
The PHP server side (testSendData.php
) is not working well, the only I managed to get is the JSON data itself but not able to get the different fields:
$jsonData = file_get_contents("php://input");
I tried using json_decode
to get fields individually without success, i also tried creating a php CustomerData class
to decode JSON but still not working.
The purpose of getting the individual fields of the JSON is to store some of them in a DB
Any help or maybe a reference tutorial would be appreciated.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…