How to Handle Status Code 302 In Post Request In Flutter ?

· 2 min read
How to Handle Status Code 302 In Post Request In Flutter
How to Handle Status Code 302 In Post Request In Flutter

In a mobile application user frequently needs to display data response on a mobile screen. So in this article, we will go through how to handle status code 302  in Post Request in Flutter.

Redirections for 302 are made in response to GET or HEAD requests, never for the POST. Sometimes server sends 302 in response to POST. In this case, Dio throws an exception you can catch. So remember to check if the server status code is 302 or maybe it’s another error.

How to Handle Status Code 302 in Post Request In Flutter?

HTTP allows you to make individual HTTP requests with minimal hassle. We can also use dio package for the same.

Users can Add followRedirects: false and validateStatus: (status) { return status < 500;} to the request, code snippet will look like a below :

var response = await Dio().post("http://myurl",
    data: requestBody,
    options: Options(
        followRedirects: false,
        validateStatus: (status) { return status < 500; }
    ),
);

This way you can get from the 302 every header and other.

try{
    await dio.post( _urlLogin,
      data:{...},
      options: Options(
        contentType: ContentType.parse("application/x-www-form-urlencoded"),          
      )
  );
}on DioError catch(error){
    if(error.response.statusCode == 302){
    // do your stuff here
     }

Conclusion:

So, in this article, we have been through how to use Stack Widget Flutter.

We hope that you enjoyed reading our post and we will keep publishing more posts every day. So, do visit us for new Flutter content every day.

Keep Learning!! Keep Fluttering !!! Stay connected.

Flutter Agency is our portal Platform dedicated to Flutter Technology and Flutter Developers. The portal is full of cool resources from Flutter like Flutter Widget GuideFlutter ProjectsCode libs and etc.

Flutter Agency is one of the most popular online portals dedicated to Flutter Technology and daily thousands of unique visitors come to this portal to enhance their knowledge of Flutter.

Leave a Reply