I want to send a POST
request with an Electron webview from the outer script. At the moment I just set the src
attribute to trigger a page load, which sends a GET
request:
<webview id="view">
<script>
document.getElementById('view').setAttribute('src', '/?foo=bar');
</script>
Is there any way to navigate the webview to a URL by sending a POST
request? Maybe a method of the webview
, instead of just hacking with the src
?
I want to send a POST
request with an Electron webview from the outer script. At the moment I just set the src
attribute to trigger a page load, which sends a GET
request:
<webview id="view">
<script>
document.getElementById('view').setAttribute('src', 'http://example./?foo=bar');
</script>
Is there any way to navigate the webview to a URL by sending a POST
request? Maybe a method of the webview
, instead of just hacking with the src
?
<form method="post">
? Could that work? Or inject that JavaScript code into the <webview>
that does that?
– flori
Commented
Dec 10, 2015 at 6:36
You can execute arbitrary code from within the webview context with .executeJavaScript
.
Moreover your code has access to all browser built-in apis. Easiest would be to use fetch
, with method set to post
.
In your case (provided the webview has been already loaded; for example its .src
has been set):
document.getElementById('view')
.executeJavaScript('fetch("http://example./?foo=bar", {method: "post"});');
Some remarks:
.src
of the webview.http:
from https:
.Now there is a new <webview>.loadURL()
method with a postData
option in the docs. I haven't used it yet but it looks exactly like what I was looking for in the past.
It seems they added it as a feature in the meantime.
Basically, Webview element does not have a property like "method" of Form so you can not specify a particular HTTP method for its request. I remend you to use AngularJS or any other JS frameworks to archive your purpose.
I found two workaround since <webview>
does not seem to currently have any way to send a POST
request.
GET
by adding any form elements to the URL's query string. It turns out the site I was using did allow this and I wouldn't've guessed had I not actually tried.POST
manually through AJAX/fetch etc then replace the HTML of the page in the webview with the HTML returned by your manual POST
. You can achieve this using .executeJavaScript()
and/or Electron's IPC.Neither workaround will work in every case. It might be worth filing a feature request with the Electron team too...
So I just went ahead and submitted a feature request. You can follow it here: https://discuss.atom.io/t/add-http-post-method-to-webview/29702