颤振:308 永久重定向响应仅在 Flutter 代码中发现的问题邮递员收到的预期回复

问题描述

以下是我通过 Flutter 请求 Post Web 服务时得到的响应。当我使用邮递员尝试相同的 Web 服务时,我得到了预期的响应。我错过了什么?

<html>
<head><title>308 Permanent Redirect</title></head>
<body>
<center><h1>308 Permanent Redirect</h1></center>
<hr><center>Nginx</center>
</body>
</html>

下面是我的代码

var params = {
"hostName": 'something',"username": 'something',"password": 'something'
}
var postBody = json.encode(params);
var uri = Uri.encodeFull('http://{{api_url}}/account/authenticate');
var response = await http.post(uri,body: postboy,headers: {HttpHeaders.contentTypeHeader: " application/json"});
print response.body;

解决方法

这条评论对我有帮助: https://stackoverflow.com/a/60957465/8013132

以下是我修改后的代码:

var params = {
    "hostName": 'something',"username": 'something',"password": 'something'
}
var postBody = json.encode(params);
var uri = Uri.encodeFull('http://{{api_url}}/account/authenticate');
var response = await http.post(uri,body: postBody,headers: {HttpHeaders.contentTypeHeader: " application/json"});
print(response.body);

//Here you get the 308 error.

final getResponse =
  await http.post(response.headers["location"],headers: {
    HttpHeaders.contentTypeHeader: "application/json",});
print(getResponse.body); //this is the expected response