【问题标题】:Flutter http Package POST API call return 302 instead of 200, post is success in DBFlutter http Package POST API 调用返回 302 而不是 200,po​​st 在 DB 中是成功的
【发布时间】:2020-07-11 19:34:16
【问题描述】:

您好,当我通过 Postman 发出 API 发布请求时,得到正确的状态码为 200,但使用 flutter/http 包 (v0.12.0+4) 或 DIO (v3.0) 进行了相同的 api 调用。 0.9) 包 我得到的状态码为 302,但发布成功并且数据已保存在数据库中。我怎样才能获得状态 200 或者有更好的方法来处理帖子中的重定向

发现了这些 git hub 问题,但没有关于如何解决此问题的答案 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 评论更新的工作代码

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

【问题讨论】:

  • 可能是从您的服务器返回状态码。在 postman 或任何其他 rest-client 中调用相同的 api 并验证状态码。 302 不是错误,它用于 url 重定向。请咨询您的 api 开发人员。
  • @midhun-mp 邮递员返回 200
  • 能否请您在隐藏网址和其他重要数据后添加该邮递员的屏幕截图和回复?
  • @midhun-mp 用 Postman 截图更新了问题
  • 查看http 包的帖子和报告的问题,似乎不支持POST 请求的重定向。您需要手动处理它,您还必须在代码中查找 http 状态代码 302,并且重定向 url 将位于该 302 响应的标头(可能)中。使用该网址并执行GET,您将获得所需的输出。

标签: flutter flutter-http


【解决方案1】:

只需添加额外的标题,

header : "Accept" : "application/json"

【讨论】:

    【解决方案2】:

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

    因此您必须手动处理重定向。在您的代码中,您还必须为状态代码 302 添加条件。当状态码为 302 时,在响应头中查找重定向 url 并对该 url 执行 http GET。

    【讨论】:

      猜你喜欢
      • 2018-05-28
      • 2011-08-30
      • 1970-01-01
      • 2011-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-09
      • 2011-11-09
      相关资源
      最近更新 更多