0

我正在尝试使用颤振登录 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":"缺少参数:用户名"}

看起来服务器无法识别我的请求的“正文”。

4

1 回答 1

0

我真的不知道为什么,但它与 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()}");
于 2021-03-24T15:08:20.600 回答