MicrosoftEdge / MicrosoftEdge/WebView2Feedback
Unable to set custom header in CoreWebView2WebResourceRequest for NavigateWithWebResourceRequest
Nobody has claimed this yet.
- Dominant language
- PowerShell
- Stars
- 526
- Forks
- 67
- PR merge metrics
- No merged PRs in 30d
Description
I am trying to set custom header to WebResourceRequest before navigating using NavigateWithWebResourceRequest API. I am using CoreWebView2WebResourceRequest API to create WebResourceRequest.
Somehow custom header is always ignored in normal execution. If I put break-point or sleep (~100 millisecond), the custom header is set properly (and visible in Inspect -> Network window). I am using WebView2 SDK version 1.0.1245.22.
Tried below work-arounds:
- Using first dummy header as mentioned in issue #1123.
- Setting headers using ICoreWebView2HttpRequestHeaders interface as suggested in MS_docs
Below is code snipped example I am using
wrl::ComPtr<ICoreWebView2WebResourceRequest> webResourceRequest;
wrl::ComPtr<ICoreWebView2HttpRequestHeaders> requestHeaders;
hr = webviewEnvironment2->CreateWebResourceRequest(
url.c_str(),
L"GET",
nullptr,
L"", // Tried setting headers as L"My-Header: 1" or L"Dummy-Header: 1\r\nMy-Header: 1\r\n"
&webResourceRequest);
if (hr.Failed())
{
return false;
}
//
hr = webResourceRequest->get_Headers(&requestHeaders);
if (hr.Succeeded() && requestHeaders)
{
for (const auto& header : httpHeaders)
{
requestHeaders->SetHeader(header.first.c_str(), header.second.c_str());
}
}
// wait for 100 milliseconds to ensure correct setting of custom headers
std::this_thread::sleep_for(std::chrono::milliseconds(100));
wrl::ComPtr<ICoreWebView2_2> webView2;
hr = m_webView.As(&webView2);
if (hr.Failed() || !webView2)
{
return false;
}
hr = webView2->NavigateWithWebResourceRequest(webResourceRequest.Get());
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the issue's CreateWebResourceRequest, get_Headers, SetHeader, and NavigateWithWebResourceRequest calls, using WebView2 SDK 1.0.1245.22. Reproduce the request with and without the 100 ms delay and inspect the custom header in the Network window. Done means the custom header is reliably included without a breakpoint or sleep.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- desktop
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100