Flutter http Package POST API调用返回302而不是200,DB中的发布成功 [英] Flutter http Package POST API call return 302 instead of 200, post is success in DB

查看:118
本文介绍了Flutter http Package POST API调用返回302而不是200,DB中的发布成功的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我通过邮递员发出API张贴请求,并获得正确的状态代码为200 时,却使用flutter/http软件包(v0.12.0 + 4)或DIO(v3)进行了相同的api调用. 0.9)程序包我得到的状态码为302,但是发布成功,并且数据已保存在DB上.我该如何获取状态200还是有更好的方法来处理帖子中的重定向

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

发现了这些git hub问题,但没有解决此问题的答案 https://github.com/dart-lang/http/issues/157

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

邮递员截图

===根据@ midhun-mp注释更新工作代码

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

推荐答案

302不是错误,它是重定向状态代码. http软件包不支持POST请求的重定向.

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

因此,您必须手动处理重定向.在您的代码中,您还必须为状态代码302添加一个条件.当状态码为302时,在响应标头中查找重定向URL,然后对该URL进行http GET.

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.

这篇关于Flutter http Package POST API调用返回302而不是200,DB中的发布成功的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆