Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Re-using QNetworkRequest in finished() slot issue
Forum Updated to NodeBB v4.3 + New Features

Re-using QNetworkRequest in finished() slot issue

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 957 Views 1 Watching
  • 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 Offline
    W Offline
    Wallboy
    wrote on last edited by Wallboy
    #1

    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 ColbyP 1 Reply Last reply
    0
    • W 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 ColbyP Offline
      Paul ColbyP Offline
      Paul Colby
      wrote on last edited by Paul Colby
      #2

      @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
      4
      • W Offline
        W Offline
        Wallboy
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved