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

Time:02-27

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.

Solved by adding

      "Content-Type": "application/json",

to the headers. Thank you.

CodePudding user response:

Use insted to do that.

Try keeping json body null the

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

CodePudding user response:

If you want to make an HTTP Request without a body. You need to use HEAD method.

  Future httHead() async {
   var baseUrl = Uri.parse("https://stackoverflow.com/");
   final response = await http.head(baseUrl,
   headers: {
     "Accept": "*/*",
   },

  );
 }
  • Related