Search code examples
dartposthttp-status-code-400

Getting 400 (Bad Request) on a POST request to Strapi


I am trying to post data to my Strapi project from a Flutter app. I made sure that the permissions are enabled for unauthenticated users. What is wrong with my request?

Future saveReview(usrReview, usrRating) async {
  const endpoint = 'http://localhost:1337/api/reviews';
  var url = Uri.parse(endpoint);
  Map<String, String> headers = {
    'Content-Type': 'application/json; charset=UTF-8',
  };
  var reviewObj = jsonEncode({
    'review': usrReview,
    'rating': usrRating,
  });

  var response = await http.post(
    url,
    headers: headers,
    body: reviewObj,
  );

  print(response.statusCode); 
}

Solution

  • The problem was that the structure of reviewObj was missing something. I had to include 'data' in it for it to work. Here is what the correct body should look like.

     var reviewObj = jsonEncode({
      'data': {
        'review': usrReview,
        'rating': usrRating,
      }
    });