I’m using the JUCE WebBrowserComponent and looking to add custom headers to certain HTTP requests. Looking at the documentation there is a headers
argument that can be used with goToURL
which is described as:
an optional set of parameters to put in the HTTP header. If you supply this, it should be a set of string in the form “HeaderKey: HeaderValue”
I couldn’t get this working in our project so rather than create a minimal application to post to the forums, I’ve simply made the following change to the JUCE example demos (examples/GUI/WebBrowserDemo.h) of the JUCE repository
diff --git a/examples/GUI/WebBrowserDemo.h b/examples/GUI/WebBrowserDemo.h
index 73c55e53f..adfecc003 100644
--- a/examples/GUI/WebBrowserDemo.h
+++ b/examples/GUI/WebBrowserDemo.h
@@ -111,8 +111,11 @@ public:
addAndMakeVisible (forwardButton);
forwardButton.onClick = [this] { webView->goForward(); };
+ std::unique_ptr<juce::StringArray> headers = std::make_unique<juce::StringArray> (
+ "Foo: Bar"
+ );
// send the browser to a start page..
- webView->goToURL ("https://www.juce.com");
+ webView->goToURL ("http://localhost:5000", headers.get());
setSize (1000, 1000);
}
I’ve created a minimal Flask app (app.py) in Python listening on http://localhost:5000 to print out any headers that are received (run with flask --app app run
):
from flask import Flask, request
app = Flask(__name__)
@app.route('/')
def index():
print(request.headers)
return 'OK', 200
When opening the DemoRunner WebBrowserComponent demo, the http://localhost:5000 address is shown and the headers are printed out from the Python application but the custom header “Foo: Bar” isn’t included:
Host: localhost:5000
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.0 Safari/605.1.15
Accept-Encoding: gzip, deflate
Accept-Language: en-GB
Connection: Keep-Alive
If I run the following Python script (with the same custom header as was added to the JUCE demo) I can see the headers are received by the web server app:
import requests
requests.get('http://localhost:5000', headers={
'Foo': 'Bar'
})
Web server app output:
Host: localhost:5000
User-Agent: python-requests/2.29.0
Accept-Encoding: gzip, deflate
Accept: */*
Connection: keep-alive
Foo: Bar
The changes to the JUCE WebBrowserDemo.h above have been tried on the latest master (69795dc8) and develop (2d31153d) branches.
Software versions:
- Ubuntu 22.10
- cmake version 3.24.2
Is there something I’m missing with how HTTP headers are supposed to be sent?