Home > Enterprise >  Flutter HTTP post request JSON handling not working properly
Flutter HTTP post request JSON handling not working properly

Time:09-22

I am using nodejs backend with flutter app. I use flutter http package to send requests. I want to send datetime via the request, so I converted it to string. So in the node backend this data cannot be converted into original types (like DateTime or int). What is the best practice to deal with JSON data in flutter and node. I want to send data (date, string, int) via http request and retrieve them in backend in correct format. Here is the flutter function. This completely converts all details to string but I cannot retrieve original formats in node backend.

Future<PlanDataInit> createItin(String? name, String? budget,
DateTime? startDate, DateTime? endDate) async {
final response = await http.post(
Uri.parse('http://localhost:3001/itinerary/createitin'),
headers: <String, String>{
  'Content-Type': 'application/json; charset=UTF-8',
},
body: jsonEncode(<String, String>{
  "Name": name.toString(),
  "startDate": startDate.toString(),
  "endDate": endDate.toString(),
  "initialBud": budget.toString()
}),
);

CodePudding user response:

use startDate.millisecondsSinceEpoch.toString()

CodePudding user response:

Convert your dates to ISOstring formats. In your NodeJS parse\decode what you encoded when you sent them from flutter.

  • Related