【问题标题】:API call is not successful using Http in flutter [closed]在flutter中使用Http调用API调用不成功[关闭]
【发布时间】:2021-05-14 01:29:24
【问题描述】:
    Future<String> APICall() async {
    var response = await http.post(Uri.encodeFull('url'),
        headers: {
          'Content-type': 'application/json',
          'Accept': 'application/json',
          "Userid": "55",
          "Deviceid": "a4021faf096eefa1",
          "Token": "b41e6d79062d4dfc2bad4d3141b6ebd6",
        });
    print(response.body);
    print(response.statusCode);
    int resCode = response.statusCode;
    if (resCode == 200) {}
  }

作为 api 的响应,它显示服务器未收到标头参数。

【问题讨论】:

  • resCode 是什么? 403?第422章?

标签: android api flutter header


【解决方案1】:

您只需在发送前对正文进行编码:

import 'dart:convert';
...

var bodyEncoded = json.encode(body);
var response = await http.post(url, body: bodyEncoded , headers: {
  "Accept": "application/json"
},);

【讨论】:

  • in response of the api it is showing that header parameters not received in server. 好像api找不到头文件,对body进行编码会有影响吗?
  • 在这个 api 调用中我不需要传递任何正文...只有标题需要传递
猜你喜欢
  • 2021-10-25
  • 2018-08-12
  • 1970-01-01
  • 2020-07-02
  • 2023-03-16
  • 2020-07-11
  • 1970-01-01
  • 1970-01-01
  • 2023-02-23
相关资源
最近更新 更多