如何解决Flutter http.post throw 405 需要 POST 方法
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,body: jsonEncode(formMap),headers: {
"Content-Type": "application/x-www-form-urlencoded"
},encoding: Encoding.getByName("utf-8"),);
看起来我的请求的“正文”没有被服务器识别。
解决方法
我真的不知道为什么,但它与 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()}");
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。