如何在flutter graphql调用中发送cookie



我正在开发Graphql QUERY API,它读取域cookie并基于返回数据,但在使用flutter应用程序时,我无法发送cookie(这是浏览器中的默认设置(。有没有更简单的方法发送cookie。

下面是我从域中读取cookie的代码片段。我认为应该有某种方法在httpLink中设置cookie/选项。

final cookieManager = WebviewCookieManager();
final gotCookies = await cookieManager.getCookies('https://someApp.com');

final HttpLink httpLink = HttpLink(
uri: 'https://someApp.com/graphql',
includeExtensions: true
);

ValueNotifier<GraphQLClient> client = ValueNotifier(
GraphQLClient(
cache: InMemoryCache(),
link: httpLink,
),
);

找到了解决方案。我们可以像下面这样在标题中设置cookie,它很有效。

HttpLink httpLink = HttpLink(
uri: 'https://apps.cloudhealthtech.com/ui-session',
headers: {'Cookie': 'session_id=ctHB0ZewfasKWBWIUu;'});

找到了一个解决方案,可以在GraphQL请求中传递浏览器cookie,而无需首先检索它们。它使用浏览器客户端

import 'package:http/browser_client.dart';

将准备好的Http客户端插入httpLink

var myClient = BrowserClient();
myClient.withCredentials =true;
final httpLink = HttpLink(endpoint +"/graphql",
httpClient: myClient,
// ...
);

最新更新