Home > Enterprise >  dart / flutter how to make http post request with empty body
dart / flutter how to make http post request with empty body

Time:02-26

I am trying to call post url but the body payload needs to be empty. While requesting the api from postman, it is working fine. However, when calling from dart http.post it is not working.

Here is my code

final response = await http.post(Uri.parse("$baseUrl/api/logout"),
headers: {
  "Accept": "*/*",
},
);

which results in 400 response code

and this is my raw http request

POST /api/logout HTTP/1.1
Host: <url>
Accept: */*

which results in 200 response code.

CodePudding user response:

    Future method() async{
var baseUrl=Uri.parse("https://stackoverflow.com/");
final response = await http.post(Uri.parse("$baseUrl/api/logout"),
  headers: {
    "Accept": "*/*",
  },
  body: jsonEncode({
        // empty body
  })
);

} }

CodePudding user response:

Use insted to do that.

Try keeping json body null the

headers: {
    "Accept": "*/*",
    "Content-Length" : 0
  }
  • Related