简体   繁体   中英

Empty Body HTTP Post request Flutter

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM