flutterhttp-redirectpermanent

Flutter : 308 Permanent Redirect response. Issue found only on Flutter code. Expected response received on postman


Below is the response I get while requesting a Post web service via Flutter. When I try the same web service using postman, I get the expected response. What am I missing?

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

Below is my code :

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;

Solution

  • This comment helped me : https://stackoverflow.com/a/60957465/8013132

    Below is my modified code :

    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"], body: postBody, headers: {
        HttpHeaders.contentTypeHeader: "application/json",
    });
    print(getResponse.body); //this is the expected response