【问题标题】:PostMan is working but http is giving 404 error in flutterPostMan 正在工作,但 http 在颤动中出现 404 错误
【发布时间】:2021-01-26 00:16:49
【问题描述】:

我正在为我的 api 使用以下代码。此 api 在 postman 中工作,但在 http flutter 中显示 404 错误代码。

我的颤振代码:

 RaisedButton(
          onPressed: () {
            apiCall();
          },
          child: Text("Press"),
        )

  Future apiCall() async {
    var body =
        jsonEncode({"filepath": "patient/reports/1602333458533-Liver.jpg"});
    try {
      await http
          .post('http://3.6.197.52:3100/downloadFile',
              headers: {"Accept": "Application/json"}, body: body)
          .then((http.Response response) => print(response.statusCode));
    } catch (e) {
      print(e);
    }
  }

它给出了错误代码 404。

以下是邮递员的结果:

Post Man result

【问题讨论】:

    标签: api flutter http flutter-web


    【解决方案1】:

    您设置了错误的标题。 Accept 标头用于确定您期望从服务器获得的结果类型。从您的屏幕截图(和数据)看起来很清楚,您会期待image/jpg。另一方面,您缺少Content-Type 标头,它定义了您随请求发送的数据类型,在您的情况下为application/json。所以服务器可能无法正确解析body。

    假设 jsonEncodeJSON.stringify 类似,您应该执行以下操作

    Future apiCall() async {
        var body =
            jsonEncode({"filepath": "patient/reports/1602333458533-Liver.jpg"});
        try {
          await http
              .post('http://3.6.197.52:3100/downloadFile',
                  headers: {"Content-Type": "application/json"}, body: body)
              .then((http.Response response) => print(response.statusCode));
        } catch (e) {
          print(e);
        }
      }
    

    【讨论】:

    • 做了相同的 json 编码发送 pdf 文件但仍然无法正常工作
    • @s.j 404 表示您使用的 url 在服务器上是未知的
    • 传递与在邮递员中传递的相同我的 api 的 post 具有 3 个查询参数和一个 body 参数。
    • 它现在不在本地
    • @s.j 显然你没有通过相同的。如果你是,你会得到同样的回应。
    猜你喜欢
    • 2017-12-27
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 2018-08-04
    • 2019-04-22
    • 1970-01-01
    • 2021-07-14
    • 2021-12-26
    相关资源
    最近更新 更多