Flutter HTTP Post返回415 [英] Flutter HTTP Post returns 415

查看:333
本文介绍了Flutter HTTP Post返回415的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 http 飞镖库的flutter应用程序中使用 Method.Post 时遇到问题。看来,当我尝试从WebAPI发布数据时,它给了我 StatusCode 415 。参见下面的代码:

I have an issue with using Method.Post on my flutter app using http dart library. It seems that when I tried to post data from my WebAPI it gaves me a StatusCode 415. See my code below:

代码登录:

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:

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

有人知道我的代码在做什么吗?

Does anyone knew whats going on my code?

推荐答案

尝试添加此新标题:

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

更新

确定现在,您需要发送json数据,例如:

Ok now you need to send json data, like this :

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

这篇关于Flutter HTTP Post返回415的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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