4

Hi when I make an API post request via Postman getting the correct status code as 200 but make the same api call with flutter/http package (v0.12.0+4) or DIO (v3.0.9) package I'm getting status code as 302, but the post was successful and the data was saved on the DB. how can I get the status 200 for this or is there a better way to handle redirect in post

Found these git hub issues, but no answer on how to fix this https://github.com/dart-lang/http/issues/157

https://github.com/dart-lang/sdk/issues/38413

Code making API post
       ........... 

      final encoding = Encoding.getByName('utf-8');
        final headers = {
          HttpHeaders.contentTypeHeader: 'application/json; charset=UTF-8',
          HttpHeaders.acceptHeader: 'application/json',
        };
        //String jsonBody = jsonEncode(feedRequest.toJsonData());
        String jsonBody = feedRequest.toJsonData();
        print('response.SubmitFeedApiRequest>:' + feedRequest.toJsonData());
        print('jsonBody:>' + jsonBody);

        String url ='https://myapp';

        final response = await http.post(url,
            headers: headers, body: jsonBody, encoding: encoding);

        print('response.statusCode:' + response.statusCode.toString());

        if (response.statusCode == 200) {
          print('response.data:' + response.body);
        } else {
          print('send failed');
        }
     ...............

Postman Screenshot enter image description here

===UPDATED WORKING CODE AS PER @midhun-mp comment

 final response = await http.post(url,
        headers: headers, body: jsonBody, encoding: encoding);

    print('response.statusCode:' + response.statusCode.toString());

    if (response.statusCode == 302) {
      //print('response.headers:' + response.headers.toString());
      if (response.headers.containsKey("location")) {
        final getResponse = await http.get(response.headers["location"]);
        print('getResponse.statusCode:' + getResponse.statusCode.toString());
        return SubmitFeedApiResponse(success: getResponse.statusCode == 200);
      }
    } else {
      if (response.statusCode == 200) {
       // print('response.data:' + response.body);
        return SubmitFeedApiResponse.fromJson(json.decode(response.body));
      }
      return SubmitFeedApiResponse(success: false);
    }
  }
Sam
  • 6,215
  • 9
  • 71
  • 90
  • Probably that status code is getting returned from your server. Call the same api in postman or any other rest-client and verify the status code. 302 is not an error, it's used for url re-direction. Check with your api developer. – Midhun MP Mar 31 '20 at 09:36
  • @midhun-mp postman return 200 – Sam Mar 31 '20 at 09:54
  • Could you please add a screenshot of that postman with response after obscuring the url and other important data ? – Midhun MP Mar 31 '20 at 10:09
  • @midhun-mp updated the question with Postman screenshot – Sam Mar 31 '20 at 10:16
  • Looking at the posts and reported issues for `http` package, it seems like the redirection for `POST` request not supported. You need to handle it manually, you have to look for http status code 302 as well in your code and the redirect url will be in the header (probably) of that 302 response. Use that url and do a `GET` and you will get the desired output. – Midhun MP Mar 31 '20 at 10:30
  • Thanks that did work, I added the code for that if you can please add your solution as an answer, so I can accept it – Sam Mar 31 '20 at 11:10
  • Thank you for updating the working code, it would help many other :) – Midhun MP Apr 01 '20 at 18:03
  • In my case, I sent a `POST` API request using `dio` package, which returned an error `302`. I issued an identical request using Postman, resulting in a successful response with a status code 200. However, when I attempted the same request in Flutter, it produced an error with a returned status code of 302. I already solved this problem, please check my comment here if you did not solve the problem yet: https://stackoverflow.com/a/76972916/16449728 – Mostafa Alazhariy Aug 24 '23 at 21:18

2 Answers2

11

Just add additional header,

header : "Accept" : "application/json"
kk.
  • 3,747
  • 12
  • 36
  • 67
Haruna Akhmad
  • 111
  • 1
  • 4
7

The 302 is not an error, it's a redirection status code. The http package won't support redirection for POST request.

So you have to manually handle the redirection. In your code you have to add a condition for status code 302 as well. When the status code is 302, look for the redirection url in the response header and do a http GET on that url.

Midhun MP
  • 103,496
  • 31
  • 153
  • 200