【问题标题】:Flutter HTTP Post returns 415Flutter HTTP Post 返回 415
【发布时间】:2018-10-01 03:15:55
【问题描述】:

我在使用 http dart 库的颤振应用程序上使用 Method.Post 时遇到问题。似乎当我尝试从我的 WebAPI 发布数据时,它给了我一个StatusCode 415。请参阅下面的代码:

代码登录:

Future<User> login(User user) async {
print(URLRequest.URL_LOGIN);
return await _netUtil.post(Uri.encodeFull(URLRequest.URL_LOGIN), body: {
  'username': user.username,
  'password': user.password
}, headers: {
  "Accept": "application/json",
}).then((dynamic res) {
  print(res.toString());
});
}

代码 NetworkUtils:

Future<dynamic> post(String url, {Map headers, body, encoding}) async {
return await http
    .post(url, body: body, headers: headers, encoding: encoding)
    .then((http.Response response) {
  final String res = response.body;
  final int statusCode = response.statusCode;

  if (statusCode < 200 || statusCode > 400 || json == null) {
    throw new Exception('Error while fetching data.');
  }

  return _decoder.convert(res);
});
}

有人知道我的代码发生了什么吗?

【问题讨论】:

    标签: dart flutter


    【解决方案1】:

    尝试添加这个新标题:

    headers: {
      "Accept": "application/json",
      "content-type":"application/json"
    }
    

    更新

    好的,现在你需要发送 json 数据,像这样:

            import 'dart:convert';
    
    
             var body = jsonEncode( {
              'username': user.username,
              'password': user.password
            });
    
    
            return await _netUtil.post(Uri.encodeFull(URLRequest.URL_LOGIN), body: body, headers: {
              "Accept": "application/json",
              "content-type": "application/json"
            }).then((dynamic res) {
              print(res.toString());
            });
            }
    

    【讨论】:

    • 尝试了您的建议,但出现错误:Bad state: Cannot set the body fields of a Request with content-type "application/json". 现在发生了什么?对不起,虽然是新来的。
    • 嗨@diegoveloper,只是想知道"content-type", "application/json" 是否应该是"content-type": "application/json"
    【解决方案2】:

    @阿尔文奎松

    我遇到了和你一样的错误并修复它,请参见下文。

    [错误]

    StateError(错误状态:无法设置内容类型为“application/json”的请求的正文字段。)

    [原因]

    当你使用Flutter插件'http.dart'方法'http.post()'时,你应该详细阅读下面的文档(注意黑色字体):

    Sends an HTTP POST request with the given headers and body to the given URL.
    [body] sets the body of the request. It can be a [String], a [List<int>] or
    a [Map<String, String>]. If it's a String, it's encoded using [encoding] and
    used as the body of the request. The content-type of the request will
    default to "text/plain".
    
    If [body] is a List, it's used as a list of bytes for the body of the
    request.
    

    如果 [body] 是 Map,则使用 [encoding] 将其编码为表单字段。请求的内容类型将设置为"application/x-www-form-urlencoded";这不能被覆盖。

    [encoding] defaults to [utf8].
    
    For more fine-grained control over the request, use [Request] or
    [StreamedRequest] instead.
    Future<Response> post(Uri url,
    {Map<String, String>? headers, Object? body, Encoding? encoding}) =>
    _withClient((client) =>
        client.post(url, headers: headers, body: body, encoding: encoding));
    

    [解决方案]

    所以只需将您的正文编码为字符串,然后您可以将标头“内容类型”设置为“应用程序/json”。 看到@diegoveloper 的代码回答了!

    【讨论】:

      猜你喜欢
      • 2017-10-26
      • 2019-01-26
      • 1970-01-01
      • 1970-01-01
      • 2015-02-12
      • 1970-01-01
      • 2022-10-04
      • 2023-04-05
      • 2021-04-24
      相关资源
      最近更新 更多