Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Solved Re-using QNetworkRequest in finished() slot issue

    General and Desktop
    2
    3
    775
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • W
      Wallboy last edited by Wallboy

      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

      Paul Colby 1 Reply Last reply Reply Quote 0
      • Paul Colby
        Paul Colby @Wallboy last edited by Paul Colby

        @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 supplied value 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.

        1 Reply Last reply Reply Quote 4
        • Paul Colby
          Paul Colby @Wallboy last edited by Paul Colby

          @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 supplied value 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.

          1 Reply Last reply Reply Quote 4
          • W
            Wallboy last edited by

            Thanks! Using an empty QByteArray() works perfect for removing headers.

            1 Reply Last reply Reply Quote 0
            • First post
              Last post