1 year ago

#388409

test-img

pauLo_0liveira

Flutter status code 200 when I use http.post

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}');
  }
}

flutter

http

http-post

http-status-codes

0 Answers

Your Answer

Accepted video resources