Closed subzero911 closed 9 months ago
@subzero911 Maybe consider using a custom HttpClient for it:
final httpLink = HttpLink(
baseUrl,
httpClient: YourHttpClient(),
);
class YourHttpClient extends http.BaseClient {
final http.Client _client;
YourHttpClient({http.Client? client}) : _client = client ?? http.Client();
@override
Future<http.StreamedResponse> send(http.BaseRequest request) async {
request.headers.addAll({
'X-Testing': '1',
'X-Testing-Try-Mock': 'BankCardsRequest;',
});
return _client.send(request);
}
}
Hope it helps.
Thanks, but it wouldn't help. We still add a HttpClient on a HttpLink creation stage and can't change it afterwards. So it won't be much different from the problem described in a starting post (even more code). I would like to see the easier way, without having to create a custom HttpClient just in order to add headers.
Just for a workaround ... When you have a HttpClient with control then you can change it if you want to. Some sudo code here for your reference:
final yourHttpClient = YourHttpClient();
yourHttpClient.setHeader(xxxx);
final httpLink = HttpLink(
baseUrl,
httpClient: yourHttpClient,
);
//....
yourHttpClient.setHeader(xxxx);
// your query here ...
maybe consider using Context
when query🤔
final options = QueryOptions(
// .....
context: Context.fromList([
HttpLinkHeaders(headers: {
'X-Testing': '1',
'X-Testing-Try-Mock': 'BankCardsRequest;',
})
]),
);
Looks like a solution
Looks like a solution
Please close the issue if you think it's already solved.
I would like to have an option to easily add custom headers to requests. The syntax I want to see:
Currently the only way to add headers it to add it on graphql client init:
I can't change headers afterwards for individual requests.