MicrosoftEdge / MicrosoftEdge/WebView2Feedback
Passing POST headers through WebResourceRequested and CreateWebResourceRequest content is null exception
Nobody has claimed this yet.
- Dominant language
- PowerShell
- Stars
- 526
- Forks
- 67
- PR merge metrics
- No merged PRs in 30d
Description
I want to be able to give GET and POST headers for specific URLs in our software. I've seen that passing GET headers and parameters is pretty easy. I though the same would be the case for POST headers, but no.
private void CoreWebView2_WebResourceRequested(object sender, CoreWebView2WebResourceRequestedEventArgs e)
{
ICS_WebIntegratie webLink = _webView2WebLinkVM.CurrentWebObject;
if (webLink != null && BaseUtil.IsFilledCollection(webLink.HeaderParameters))
{
foreach (ICS_WebHeaderParameter item in webLink.HeaderParameters)
{
if (item.HeaderTypeId == e.Request.Method || item.HeaderTypeId == WebIntegratie_DD.HEADERPARAM_VELD_Type_BOTH)
{
e.Request.Headers.SetHeader(item.Name, item.Value);
}
}
}
}
A ICS_WebHeaderParameter can be POST, GET or both. In the case of GET, everything works like a charm. But in case of POST, the POST headers which are added to e.Equest.Headers are not received.
I saw that there was an alternative solution, which is to use WebView2.CoreWebView2.Environment.CreateWebResourceRequest(). The problem I am encountering with that is that the value for content is always unacceptable. If I add a portion to the above code, e.g.:
if (e.Request.Method == WebIntegratie_DD.HEADERPARAM_VELD_Type_POST)
{
string headers = string.Empty;
foreach (System.Collections.Generic.KeyValuePair<string, string> item in e.Request.Headers)
{
headers += $"{item.Key}: {item.Value}\n";
}
foreach (ICS_WebHeaderParameter item in webLink.HeaderParameters)
{
if (item.HeaderTypeId == e.Request.Method || item.HeaderTypeId == WebIntegratie_DD.HEADERPARAM_VELD_Type_BOTH)
{
headers += $"{item.Name}: {item.Value}\n";
}
}
UTF8Encoding encoding = new UTF8Encoding();
byte[] postdata = encoding.GetBytes("test");
Stream stream = new MemoryStream(postdata);
CoreWebView2WebResourceRequest req = (sender as WebView2Wpf.WebView2).CoreWebView2.Environment.CreateWebResourceRequest(e.Request.Uri, e.Request.Method, stream, headers);
(sender as WebView2Wpf.WebView2).CoreWebView2.NavigateWithWebResourceRequest(req);
return;
}
I always get a 'System.NullReferenceException' when the variable 'stream' is not null for example.
How can I pass POST HTTP headers to my web requests?
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 CoreWebView2_WebResourceRequested handler and the CreateWebResourceRequest and NavigateWithWebResourceRequest calls shown in the issue. Reproduce the NullReferenceException with a non-null MemoryStream and inspect how POST headers and content are passed. Done means identifying the supported way to preserve POST content while adding headers, or documenting the limitation and workaround.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- api, desktop
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100