2

I am using http client for flutter network call.

  1. My request working on postman getting response properly,
  2. But while trying with http.post it returns error code 307-Temporary Redirect,

Method Body:

static Future<http.Response> httpPost(
  Map postParam, String serviceURL) async {
Map tempParam = {"id": "username", "pwd": "password"};
var param = json.encode(tempParam);
serviceURL = "http:xxxx/Login/Login";

// temp check
Map<String, String> headers = {
  'Content-Type': 'application/json',
  'cache-control': 'no-cache',
};
await http.post(serviceURL, headers: headers, body: param).then((response) {
  return response;
});
}

Also, the same code returns a proper response to other requests and URLs. First I trying with chopper client but had same issue.

I am unable to detect that issue from my end of from server-side.

Any help/hint will be helpful

6 Answers 6

19

Try to put a slash / at the end of the serviceUrl. So, serviceUrl is serviceURL = "http:xxxx/Login/Login/" instead of serviceURL = "http:xxxx/Login/Login".

This works for me.

Sign up to request clarification or add additional context in comments.

3 Comments

this one solved my 2 hours of searching for a solution. thank you.
thank you sir, I don't know why this happening tho
worked!!! fascinating. Even claude couldn't help.
1

You need to find a way to follow redirect.

Maybe postman is doing that.

Read this >> https://api.flutter.dev/flutter/dart-io/HttpClientRequest/followRedirects.html

Can you try with using get instead of post? At least to try and see what happend

In the documentation said:

Automatic redirect will only happen for "GET" and "HEAD" requests
only for the status codes 

HttpStatus.movedPermanently (301), 
HttpStatus.found (302), 
HttpStatus.movedTemporarily (302, alias for HttpStatus.found), 
HttpStatus.seeOther (303),
HttpStatus.temporaryRedirect (307)

Comments

1

@Abel's answer above is correct but I had to switch from using:

Uri url = Uri.https(defaultUri, path);

to

Uri url = Uri.parse('https://todo-fastapi-flutter.herokuapp.com/plan/');

to get that last / after plan.

The first way kept dropping it so I was getting 307 errors.

flutter.dev shows a full example:

Future<http.Response> createAlbum(String title) {
  return http.post(
    Uri.parse('https://jsonplaceholder.typicode.com/albums'),
    headers: <String, String>{
      'Content-Type': 'application/json; charset=UTF-8',
    },
    body: jsonEncode(<String, String>{
      'title': title,
    }),
  );
}

here: https://flutter.dev/docs/cookbook/networking/send-data#2-sending-data-to-server

Comments

1

Putting / at the end of the URL worked for me.

Example: https://api.com/v1/book/list gives me a temporary redirect error; while, https://api.com/v1/book/list/ worked. (/ at the end)

Comments

0

keeping https instead of http in the URL it is helping me.

Comments

0

For Dio Http Client, use Dio Option follow redirect as True

getDioOption(){
return BaseOptions(connectTimeout: 30, receiveTimeout: 30,
followRedirects: true);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.