Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter POST request body not sent

Tags:

flutter

dart

I want to make a post request in Flutter with the following code:

// Body: {"email": "[email protected]", "pass": "passw0rd"}

Future<dynamic> post(String url, var body) async {
  var response = await http.post(url, body: body);
  final String res = response.body;
  return res;
}

// That's not the full code. I removed some lines because they are useless for this thread.
// Most of them are only some debug outputs or conditional statements

The problem is that my post request doesn't include the body with my request. I checked that with some outputs on my server.

like image 350
Keanu Hie Avatar asked Dec 15 '18 11:12

Keanu Hie


1 Answers

You just have to encode the body before sending:

import 'dart:convert';
...

var bodyEncoded = json.encode(body);
var response = await http.post(url, body: bodyEncoded , headers: {
  "Content-Type": "application/json"
},);
like image 116
diegoveloper Avatar answered Oct 20 '22 18:10

diegoveloper