-1

I'm creating an app on Flutter and when I use http.post the status code that flutter returns is 200... everytime.

When there's an actual error, from user input it returns 200, but when the data is correct and inserted into the database it should return status code 201, right?

Future<Event> createEvent(
  String eventTitle,
  String eventDescription,
  String startDate,
  String endDate,
  String avenue,
  int maxMembers,
) async {
  final response = await http.post(
    Uri.parse('http://192.168.101.132:8080/api/event'),
    headers: {
      "Content-Type": "application/json; charset=utf-8",
      "Accept": "application/json",
    },
    body: json.encode({
      'eventTitle': eventTitle,
      'eventDescription': eventDescription,
      'startDate': startDate,
      'endDate': endDate,
      'avenue': avenue,
      'maxMembers': maxMembers,
    }),
  );
  print(response.statusCode);

  if (response.statusCode == 201) {
    return Event.fromJson(json.decode(response.body));
  } else {
    throw Exception('Failed to CREATE event: ${response.body}');
  }
}
3
  • 3
    Are you certain the backend is sending the status code back correctly? Commented Apr 7, 2022 at 17:07
  • 1
    I believe there's a possibility this is an issue in the API and not Flutter. I'd test this API by trying to make the POST call via Postman or cURL with all the same values in the JSON body as your flutter API call. If you're still getting a 200 response instead of an error when using Postman or cURL, this would confirm that this is an issue on the API backend, and not on Flutter. Usually you'll need to manually check for errors on the API backend and send an error status code if an error is detected from the API side. Commented Apr 7, 2022 at 17:39
  • 1
    Thank you Evert and Jeith. My API wasn't sending the correct status codes. I've fixed it and now it's working. :) Commented Apr 8, 2022 at 8:24

1 Answer 1

0

The status codes 200 and 201 indeed indicates that the HTTP request is successful. However, you need to verify that the API works correctly as well. Another way to validate the REST API that you're using is by using tools like Postman to send out HTTP requests for testing.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.