Dio: DioError [DioErrorType.RESPONSE]: Http status error [302]

Created on 20 Apr 2019  路  6Comments  路  Source: flutterchina/dio

hi !

when im using dio post,
the post was successfull and the data was saved on the db.

but i check the logs, and got error code

DioError [DioErrorType.RESPONSE]: Http status error [302]

here's my code

_submit() async {    
var url =
        "https://script.google.com/macros/s/AKfycbyZMw-4aNli_vfIucxNxAZvIfWOtWlQEBST8EtDh4zGcDnEHCA7/exec?";

    try {
      var dio = new Dio();
      var response = await dio.post(
        url,
        queryParameters: {
          'action': 'insert',
          'tableName': 'presensi',
          'nis': nis,
          'nama': nama,
          'status': status
        },
        options: Options(
          connectTimeout: 5000,
          receiveTimeout: 10000,
        ),
      );

      print(response.toString());
    } on DioError catch (error) {
      if (error.response.statusCode == 302) {
        print("nani kore ? : " + error.toString());
      }
    }

note :
post was success but logs show 302.
how to resolve 302 code?

stale

Most helpful comment

i have a same question too

All 6 comments

302 is the status code for a redirect.
I face the kind a same issue. My POST request to log in is awnserd with a 302 response. But i'm not sure wether the responded cockie is saved to the cockie jar.
The followup requests fail cause the cockie is not present.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, please make sure it is up to date and if so, add a comment that this is still an issue to keep it open. Thank you for your contributions.

i have a same question too

I have the same concern as @zaubererty regarding cookies. Dio makes a POST request and receives a 302 response with a Set-Cookie header. However the redirected request does not appear to include the new cookie. I'm using the default CookieJar plugin with a PersistentCookieJar.

Add followRedirects: false and validateStatus: (status) { return status < 500;} to the request

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

Source https://stackoverflow.com/questions/52537500/flutter-handle-status-code-302-in-post-request

Thanks @alfawzaan
I'd suggest return status < 400; , as you probably want 404 not found , 401 unauthorized , etc to still raise errors. Also keep in mind if you aren't following redirects you'll need a manual way to send Dio to the relocated page, perhaps by reading the Location header

Was this page helpful?
0 / 5 - 0 ratings

Related issues

developerandre picture developerandre  路  6Comments

neeraj87 picture neeraj87  路  4Comments

lazarvgd picture lazarvgd  路  4Comments

codeThe0 picture codeThe0  路  6Comments

Dmitry-N-Medvedev picture Dmitry-N-Medvedev  路  3Comments