flutterdartflutter-httpdart-http

Flutter does not handle http redirect correctly


Hi I have the following problem in flutter. I make a post http request and in the browser the website returns status 302, with the location of the redirect in the header of the response called "location", so far so good. But when I make the request in flutter the response status is 301, and I don't get the redirect link in the header

I only get in the "location" header the url from where I made the request, not the new url to which it should be redirected

This is the part of my code that makes the request

http.Request req = http.Request("Post", uri)..followRedirects = false;
 
req.body = 'criterio=3&valor=$codigo&nombre_apellidos=&op=Buscar&form_build_id=$idForm&form_id=tracking_form';
http.Client baseClient = http.Client();
http.StreamedResponse response = await baseClient.send(req);

print(response.statusCode);
print(response.headers);

Solution

  • This is a problem with your backend, not with Flutter or the http package. It does not change HTTP response codes or headers.

    As for the cause of the different responses between your browser and your code, something about your request is causing this.

    Perhaps your request body is malformed - it's not being formed in a very robust way. What of one of the values has an ampersand in it?

    Otherwise, it could be a matter of incorrect request headers. Your request looks like a form submission, but that should be include a header describing the body format.