I want to port my app from Qt WebKit to Qt WebEngine. In Qt Webkit I can set http headers by using QNetworkRequest, but in Qt WebEngine, the doc says:
Qt WebEngine has its own HTTP implementation and cannot go through a QNetworkAccessManager
I use PyQt5 and Qt5.8.
I cannot find how to set http headers in Qt WebEngine.
--------------update----------------
Finally, it worked! Thanks! @Trilarion:
define my QWebEngineUrlRequestInterceptor class
class NWUrlRequestInterceptor(QWebEngineUrlRequestInterceptor):
def __init__(self, headers):
super(QWebEngineUrlRequestInterceptor, self).__init__()
self.headers = headers
def set_headers(self,headers):
self.headers = headers
def interceptRequest(self, info):
print info, self.headers
for header, value in self.headers:
info.setHttpHeader(header, value);
use in my browser:
self.request_interceptor = NWUrlRequestInterceptor(self.headers)
self.webpage.profile().setRequestInterceptor(self.request_interceptor)
You should implement a QWebEngineUrlRequestInterceptor in whose interceptRequest method you get a QWebEngineUrlRequestInfo which can set a http header via setHttpHeader.
Set the request interceptor via QWebEngineView.page().profile().setRequestInterceptor.
Inspired by How to send HTTPHeader using QT WebEngine?.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With