RanH
RanH

Reputation: 852

Why flutter http response in web missing headers?

I am making a simple flutter app using http package. I am trying to send a (post) login request, and the httpresponse is missing all the headers! this works fine in ios simulator but in chrome as you can see there are only 2 headers available; enter image description here

enter image description here

What is wrong? Thanks

Upvotes: 2

Views: 590

Answers (2)

Petr B.
Petr B.

Reputation: 891

Setting the header param "Access-Control-Expose-Headers" = "*" from the server side did the trick.

Please see the issue detail here: https://github.com/dart-lang/http/issues/726#issuecomment-1433169316

Upvotes: 1

JJGV
JJGV

Reputation: 1

I have the following:

Response headers web:

"Origin":"http://localhost:57986"

Example code:

Future<File> downloadFile(String url, String pathFile) async {
     File file = new File(pathFile);
     var request = await http.post(Uri.parse(url), encoding: Encoding.getByName('utf8'), headers: {
       "Access-Control-Allow-Origin": "*",
       "Access-Control-Allow-Methods": "POST, GET, OPTIONS, PUT, DELETE, HEAD",
     });
     if (request.statusCode == 200) {
       Map<String, String> headers = request.headers;
       if (headers.containsKey("Origin")) {
         var origin = headers['Origin'];
         print("your result: " + origin.toString());
       }
       file.writeAsBytes(request.bodyBytes);
     }
     return file;
   }

Upvotes: 0

Related Questions