Home > Enterprise >  Empty Body HTTP Post request Flutter
Empty Body HTTP Post request Flutter

Time:04-29

I am trying to send a Post request but the body of the request is meant to be empty and I have tried the endpoint on postman with an empty body and it works.

But now I don't know how to implement that on Flutter.

I know how to create post requests with body. But I don't know how to make the post with an empty body.

Future<http.Response> createAlbum() {
  return http.post(
    Uri.parse(BASE_URL),
    headers: <String, String>{
      'Content-Type': 'application/json; charset=UTF-8',
    },
    body: jsonEncode(<String, String>{
      
    }),
  );
}

when the response body is empty the response.statusCode is 500

CodePudding user response:

I figured it out, http allows an empty parameter to be passed as the argument to a body.

var response = await client.post(
        uri,
        body: jsonEncode({}),
        headers: {
          'Content-Type': 'application/json',
          'Authorization': 'Bearer $token',
    },
);

CodePudding user response:

Future<http.Response> createAlbum() {
  return http.post(
    Uri.parse(BASE_URL),
    headers: <String, String>{
      'Content-Type': 'application/json; charset=UTF-8',
    },
     body: jsonEncode("{}"),
  );
}
  • Related