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. Accessing headers/cookies in WebSockets handshake request
QtWS25 Last Chance

Accessing headers/cookies in WebSockets handshake request

Scheduled Pinned Locked Moved Solved General and Desktop
websocketservercookiesheaders
5 Posts 2 Posters 1.6k Views
  • 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.
  • C Offline
    C Offline
    Cocojambo
    wrote on 6 Oct 2020, 06:54 last edited by
    #1

    Hi. I'm playing with Websockets Simplechat example from here QT Simplechat:

    It's works correctly but how can I access cookies?

    The docs says that we can access/use it Mozilla MDN

    Also, common headers like User-Agent, Referer, Cookie, or authentication headers might be there as well.
    

    And I see that Mozilla (Opera) transfers Cookie header.

    P 1 Reply Last reply 6 Oct 2020, 13:21
    0
    • C Cocojambo
      6 Oct 2020, 06:54

      Hi. I'm playing with Websockets Simplechat example from here QT Simplechat:

      It's works correctly but how can I access cookies?

      The docs says that we can access/use it Mozilla MDN

      Also, common headers like User-Agent, Referer, Cookie, or authentication headers might be there as well.
      

      And I see that Mozilla (Opera) transfers Cookie header.

      P Offline
      P Offline
      Pablo J. Rogina
      wrote on 6 Oct 2020, 13:21 last edited by
      #2

      @Cocojambo said in Accessing headers/cookies in WebSockets handshake request:

      QT Simplechat:

      In that example, you can have the QWebSocket from every connection

      QWebSocket *pSender = qobject_cast<QWebSocket *>(sender());

      so with QWebSocket::request() you can have the QNetworkRequest for such connection
      and ultimately you'll get the headers from that request:

      QNetworkRequest::header(QNetworkRequest::KnownHeaders header)
      QNetworkRequest::rawHeader(const QByteArray &headerName)

      Upvote the answer(s) that helped you solve the issue
      Use "Topic Tools" button to mark your post as Solved
      Add screenshots via postimage.org
      Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

      C 1 Reply Last reply 6 Oct 2020, 16:12
      3
      • P Pablo J. Rogina
        6 Oct 2020, 13:21

        @Cocojambo said in Accessing headers/cookies in WebSockets handshake request:

        QT Simplechat:

        In that example, you can have the QWebSocket from every connection

        QWebSocket *pSender = qobject_cast<QWebSocket *>(sender());

        so with QWebSocket::request() you can have the QNetworkRequest for such connection
        and ultimately you'll get the headers from that request:

        QNetworkRequest::header(QNetworkRequest::KnownHeaders header)
        QNetworkRequest::rawHeader(const QByteArray &headerName)

        C Offline
        C Offline
        Cocojambo
        wrote on 6 Oct 2020, 16:12 last edited by
        #3

        @Pablo-J-Rogina
        Thank you, your answer helped me. Something like this returns all cookies as raw string.

        QNetworkRequest rq = pSocket->request();
        QByteArray h = rq.rawHeader("Cookie");
        qDebug() << QString::fromUtf8(h);
        

        Could you also advice how to iterate all cookies from the header?

        QVariant hdr = rq.header(QNetworkRequest::CookieHeader);
        

        qDebug() << hdr returns QVariant(QList<QNetworkCookie>, ) and in debugger I see that it contains several items (the same qty as browser sends).

        P 1 Reply Last reply 6 Oct 2020, 16:29
        0
        • C Cocojambo
          6 Oct 2020, 16:12

          @Pablo-J-Rogina
          Thank you, your answer helped me. Something like this returns all cookies as raw string.

          QNetworkRequest rq = pSocket->request();
          QByteArray h = rq.rawHeader("Cookie");
          qDebug() << QString::fromUtf8(h);
          

          Could you also advice how to iterate all cookies from the header?

          QVariant hdr = rq.header(QNetworkRequest::CookieHeader);
          

          qDebug() << hdr returns QVariant(QList<QNetworkCookie>, ) and in debugger I see that it contains several items (the same qty as browser sends).

          P Offline
          P Offline
          Pablo J. Rogina
          wrote on 6 Oct 2020, 16:29 last edited by
          #4

          @Cocojambo said in Accessing headers/cookies in WebSockets handshake request:

          qDebug() << hdr returns QVariant(QList<QNetworkCookie>, )

          Well, your "problem" now turned into iterating a list of QNetworkCookie items... some pseudo-code:

          foreach (QNetworkCookie cookie, hdr) {
              qDebug() << cookie.toRawForm();
          }
          

          Upvote the answer(s) that helped you solve the issue
          Use "Topic Tools" button to mark your post as Solved
          Add screenshots via postimage.org
          Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

          C 1 Reply Last reply 7 Oct 2020, 07:46
          3
          • P Pablo J. Rogina
            6 Oct 2020, 16:29

            @Cocojambo said in Accessing headers/cookies in WebSockets handshake request:

            qDebug() << hdr returns QVariant(QList<QNetworkCookie>, )

            Well, your "problem" now turned into iterating a list of QNetworkCookie items... some pseudo-code:

            foreach (QNetworkCookie cookie, hdr) {
                qDebug() << cookie.toRawForm();
            }
            
            C Offline
            C Offline
            Cocojambo
            wrote on 7 Oct 2020, 07:46 last edited by
            #5

            @Pablo-J-Rogina

            Accidentally found needed code in QT sources. It returns all the cookies one by one.

            QVariant hdr = rq.header(QNetworkRequest::CookieHeader);
            
            QList<QNetworkCookie> cookies = qvariant_cast<QList<QNetworkCookie> >(hdr);
            QList<QNetworkCookie>::ConstIterator it = cookies.begin(), end = cookies.end();
            
            for ( ; it != end; ++it) {
                qDebug() << it->name();
            }
            
            1 Reply Last reply
            1

            4/5

            6 Oct 2020, 16:29

            • Login

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