flutter - Flutter http.post 抛出 405 需要 POST 方法
问题描述
我正在尝试使用颤振登录 API。这是方法:
var result = await http.post(
Uri.https(host, url, queries),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body: jsonEncode(<String, String>{
'username': myUsername,
'password': myPassword,
}),
);
带有 405 错误的请求结果说:
This method requires HTTP POST
请问,我该如何处理?
编辑:
这似乎有效:
Map<String, String> formMap = {
'username': 'myUsername',
'password': 'myPassword',
};
http.Response response = await http.post(
Uri.https(host, url, queries),
body: jsonEncode(formMap),
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
encoding: Encoding.getByName("utf-8"),
);
- 状态码 200
- 响应正文 = {"stat":"fail","err":1002,"message":"缺少参数:用户名"}
看起来服务器无法识别我的请求的“正文”。
解决方案
我真的不知道为什么,但它与 multipartRequest 一起使用:
var request = http.MultipartRequest('POST', Uri.https(host, url, queries))
..fields['username'] = 'myUsername'
..fields['password'] = 'myPassword'
..headers['Content-Type'] = "application/x-www-form-urlencoded";
var res = await request.send();
print("STATUS CODE = ${res.statusCode}");
print("Response headers = ${res.headers}");
print("Response body = ${res.stream.bytesToString()}");
推荐阅读
- c - 为什么我的输出给出一个垃圾值?
- ruby-on-rails - 我可以用 number_of_days.days.ago.to_date 替换 (Date.current - created_at.to_date).to_i <= number_of_days 吗?这是个好主意吗?
- android - 刚刚添加了新片段,但它在 android studio 中显示为灰色?
- node.js - at=错误代码=H10 desc="应用程序崩溃"方法=获取路径="/"
- python - 温度换算表
- c# - 玩家跳到平台边缘后滑下平台
- elasticsearch - elasticsearch - 一秒的刷新间隔
- python - 模拟依赖
- mysql - MySql:WHERE NOT EXISTS(跳过重复项)与 group by
- python - 循环遍历唯一值以比较 Pandas 中的组