Solved Re-using QNetworkRequest in finished() slot issue
-
EDIT: Ok I figured out what was happening. My original request is a POST request using QHttp* classes. This automatically adds the Content-Type, Content-Length, MIME-Version headers afterwards. So when I copy the original request and try to use a get() request with these headers that are not supposed to be present for GETs, Qt doesn't know what to do. So now my actual question is, how can I remove headers from a QNetworkRequest object?
Ok I'm officially lost on why this is not working. I have a QNAM handling my requests and using the QNAM finished() slot. Within this slot I am handling redirects by doing the following in PyQt:
EDIT: I tried saving the original request as self.request and then using it as 'new_request = QNetworkRequest(self.request)' This works! But why can't I use reply.request()?!def slotFinished(self, reply): // If being redirected... new_request = QNetworkRequest(reply.request()) // I want a copy of the original request with all it's set custom headers, etc. new_request.setUrl('http://RedirectedUrl.com') // The only thing I change is the new URL we're being redirected to. new_request2 = QNetworkRequest(QUrl('http://RedirectedUrl.com')) // Why does this work, and not the above? self.manager.get(new_request) // This does not do anything self.manager.get(new_request2) // Works no problem
I'm clueless to why this is happening...
Any help would be appreciated
-
@Wallboy said in Re-using QNetworkRequest in finished() slot issue:
So now my actual question is, how can I remove headers from a QNetworkRequest object?
While it doesn't appear to be documented (so use at your own risk), QNetworkRequest::setHeader
and QNetworkRequest::setRawHeader() both unset headers if the suppliedvalue
is NULL.You can see this in the Qt code here (for setHeader), and here and here (for setRawHeader).
So you could do:
new_request.setHeader(QNetworkRequest::ContentTypeHeader, QVariant()); new_request.setHeader(QNetworkRequest::ContentLengthHeader, QVariant()); new_request.setRawHeader("MIME-Version", QByteArray());
Depending on your use case, you might be better off defining an explicit "white list" of safe headers you want to keep, such as:
QList<QByteArray> whitelist; whitelist.append("good-header-1"); whitelist.append("good-header-2"); foreach (const QByteArray &headerName, new_request.rawHeaderList()) { if (!whitelist.contains(headerName)) { new_request.setRawHeader(headerName, QByteArray()); } }
Or you could copy, instead of delete, the whitelisted headers like:
foreach (const QByteArray &headerName, original_request.rawHeaderList()) { if (whitelist.contains(headerName)) { new_request.setRawHeader(headerName, original_request.rawHeader(headerName)); } }
Cheers.
-
@Wallboy said in Re-using QNetworkRequest in finished() slot issue:
So now my actual question is, how can I remove headers from a QNetworkRequest object?
While it doesn't appear to be documented (so use at your own risk), QNetworkRequest::setHeader
and QNetworkRequest::setRawHeader() both unset headers if the suppliedvalue
is NULL.You can see this in the Qt code here (for setHeader), and here and here (for setRawHeader).
So you could do:
new_request.setHeader(QNetworkRequest::ContentTypeHeader, QVariant()); new_request.setHeader(QNetworkRequest::ContentLengthHeader, QVariant()); new_request.setRawHeader("MIME-Version", QByteArray());
Depending on your use case, you might be better off defining an explicit "white list" of safe headers you want to keep, such as:
QList<QByteArray> whitelist; whitelist.append("good-header-1"); whitelist.append("good-header-2"); foreach (const QByteArray &headerName, new_request.rawHeaderList()) { if (!whitelist.contains(headerName)) { new_request.setRawHeader(headerName, QByteArray()); } }
Or you could copy, instead of delete, the whitelisted headers like:
foreach (const QByteArray &headerName, original_request.rawHeaderList()) { if (whitelist.contains(headerName)) { new_request.setRawHeader(headerName, original_request.rawHeader(headerName)); } }
Cheers.
-
Thanks! Using an empty QByteArray() works perfect for removing headers.