dart-lang / dart-lang/http

[Web] Use `withCredentails = true` by default

Open
#484 5 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

type-enhancement
Dominant language
Dart
Stars
1.1k
Forks
419
Avg merge
4d 14h
Merged PRs (30d)
8

Description

Hello,

Following[ this stackoverflow issue](https://stackoverflow.com/questions/64241766/how-to-get-a-cookie-in-flutter-web-from-header) that Someone created, i have the exact same problem :
I have a problem with HTTP request on the latest beta version of Flutter Web in 1.22 (Flutter 1.22.0-12.4.pre).
I have a server that requires a login, give back a cookie, and we need to use the cookie in each subsequent calls to be authentified.

The problem is that the browser doesn't register the cookie even if it shows the set-cookie attribute in the network tab of chrome :

![image](https://user-images.githubusercontent.com/19304535/95836832-31d45880-0d40-11eb-8e87-c38a5d7782f2.png)

But when i search for the set-cookie of the response header it is null.

When i do the next API Call, the servers logs says that it doesn't received the Cookie and we can see in the network tab that it's right

![image](https://user-images.githubusercontent.com/19304535/95837274-b7580880-0d40-11eb-9b76-13f2324c14e5.png)

and the really weird thing is that the error that is written in the Chrome console is a CORS policy error even thought, it's not the problem at all because the login call works well everytime and the server have log about the second one and says that the error is the lack of cookie.

![image](https://user-images.githubusercontent.com/19304535/95837652-1f0e5380-0d41-11eb-9434-6dda6aafe828.png)

I tried to add the cookie to the browser using document.cookie and i also tried to add the Cookie Header to the HTTP request once the login is done but nothing helps and the server still does not receive the cookie.

To reproduce i just created a new flutter web project on 1.22 beta, deleted the counter and have just 3 buttons available .

Login works well everytime, twoFactorCode is the two way authentification so just after login, and it doesn't work because it's waiting for the cookie, lastly i have a API request that doesn't require the cookie or the login to be performed, and it also works everytime.

here is the code for the API calls:

```dart
import 'package:http/http.dart' as http;

var headers = {'Content-Type': 'application/json'};

login() async {
try {
var body = jsonEncode({
"name": "XXX",
"pass": "XXX",
"clientId": "XXX",
"deviceOs": "deviceOs",
"deviceName": "deviceName"
});
final response =
await http.post('http://localhost:18080/services/login', body: body, headers: widget.headers);
print(response.body);
} catch (error) {
print(error);
}
}

twofactorCode() async {
try {
var body = jsonEncode({"code": "XXXXXX"});
final response =
await http.post('http://localhost:18080/services/twofactorcode', body: body, headers: widget.headers);
print(response.body);
} catch (er) {
print(er);
}
}

getParams() async {
try {
var response = await http.get("http://localhost:18080/services/parameters");
print(response.body);
} catch (er) {
print(er);
}
}
```

Any help to know how to use the cookie for each request would be much appreciated,
thanks !

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the three HTTP calls in a Flutter Web project using package:http, focusing on how the login response cookie and subsequent requests are handled. Done means the browser sends the cookie on later authenticated requests by default, while the CORS behavior and unauthenticated parameters request remain understood and verified.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart, flutter
Domain
networking, web-dev
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.