5

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);
    }
  }
8
  • 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. Commented Mar 31, 2020 at 9:36
  • @midhun-mp postman return 200 Commented Mar 31, 2020 at 9:54
  • Could you please add a screenshot of that postman with response after obscuring the url and other important data ? Commented Mar 31, 2020 at 10:09
  • @midhun-mp updated the question with Postman screenshot Commented Mar 31, 2020 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. Commented Mar 31, 2020 at 10:30

3 Answers 3

12

Just add additional header,

header : "Accept" : "application/json"
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

0

Just add "/" to the end of your URI

String url ='https://myapp/';

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

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.