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. QNAM query fails but ReST passes
Forum Updated to NodeBB v4.3 + New Features

QNAM query fails but ReST passes

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 4 Posters 306 Views 2 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.
  • ocgltdO Offline
    ocgltdO Offline
    ocgltd
    wrote on last edited by ocgltd
    #1

    I am using QNetworkAccessManager to access a ReST service. All works fine from the command line using "curl".

    However, when run from QNAM the ReST service repies with

    "{"message":"invalid method"}"
    

    The URL I pass to QNAM is identical to that provided to curl. Is there some other header/parameter I must set for a ReST query to work?

    1 Reply Last reply
    0
    • C ChrisW67

      @hskoglund said in QNAM query fails but ReST passes:

      usually you'll write some json into the postData.

      Especially since this is what @ocgltd told the server the payload was going to be:

      request.setHeader(QNetworkRequest::ContentTypeHeader,"application/json");
      
      ocgltdO Offline
      ocgltdO Offline
      ocgltd
      wrote on last edited by
      #6

      Ok figured it out...(based on some generic 405 error information on StackOverflow). This error is common when using POST instead of GET (or vice versa).

      I just assumed they were interchangable - but I was wrong. Well, maybe this will help someone else....

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi,

        Whiteout knowing your request nor the QNAM code, it's not possible to help.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        ocgltdO 1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          Whiteout knowing your request nor the QNAM code, it's not possible to help.

          ocgltdO Offline
          ocgltdO Offline
          ocgltd
          wrote on last edited by ocgltd
          #3

          @SGaist I notice that when I use CURL, the server records a reply with 405 code. But when run from my C code it replies with a 200 code. In case that's a clue.

          I have posted my code below...hopefully something stands out...

          ```
          QUrlQuery postData;
          for (QPair<QString,QString> dataPair : m_queryItems) {
              postData.addQueryItem(dataPair.first, dataPair.second);
          }
          
          QNetworkRequest request;
          request.setUrl(QUrl(m_URL));
          qDebug() << QUrl(m_URL).toString();
          
          request.setHeader(QNetworkRequest::ContentTypeHeader,"application/json");
          
          if (!m_authUsername.isEmpty()) {
              // HTTP Basic authentication header value: base64(username:password)
              QString authInfo = m_authUsername + ":" + m_authPassword;
              QByteArray authInfoB64 = authInfo.toLocal8Bit().toBase64();
              QString authHeaderData = "Basic " + authInfoB64;
              request.setRawHeader("Authorization", authHeaderData.toLocal8Bit());
          }
          
          m_NAMobject = new QNetworkAccessManager();
          connect(m_NAMobject, &QNetworkAccessManager::finished, this, &TL_RQ_Normal::slot_replyFinished, Qt::QueuedConnection);
          connect(m_NAMobject, &QNetworkAccessManager::sslErrors, this, &TL_RQ_Normal::slot_checkVersion_SSLErrors, Qt::QueuedConnection);
          m_replyPtr = m_NAMobject->post(request, postData.toString(QUrl::FullyEncoded).toUtf8());
          
          1 Reply Last reply
          0
          • hskoglundH Offline
            hskoglundH Offline
            hskoglund
            wrote on last edited by
            #4

            Hi, it seems a bit odd the postData is a QUrlQuery with name=value pairs, usually you'll write some json into the postData.
            Also, are you setting the -X POST switch in your curl command line?

            C 1 Reply Last reply
            0
            • hskoglundH hskoglund

              Hi, it seems a bit odd the postData is a QUrlQuery with name=value pairs, usually you'll write some json into the postData.
              Also, are you setting the -X POST switch in your curl command line?

              C Offline
              C Offline
              ChrisW67
              wrote on last edited by ChrisW67
              #5

              @hskoglund said in QNAM query fails but ReST passes:

              usually you'll write some json into the postData.

              Especially since this is what @ocgltd told the server the payload was going to be:

              request.setHeader(QNetworkRequest::ContentTypeHeader,"application/json");
              
              ocgltdO 1 Reply Last reply
              0
              • C ChrisW67

                @hskoglund said in QNAM query fails but ReST passes:

                usually you'll write some json into the postData.

                Especially since this is what @ocgltd told the server the payload was going to be:

                request.setHeader(QNetworkRequest::ContentTypeHeader,"application/json");
                
                ocgltdO Offline
                ocgltdO Offline
                ocgltd
                wrote on last edited by
                #6

                Ok figured it out...(based on some generic 405 error information on StackOverflow). This error is common when using POST instead of GET (or vice versa).

                I just assumed they were interchangable - but I was wrong. Well, maybe this will help someone else....

                1 Reply Last reply
                0
                • ocgltdO ocgltd has marked this topic as solved on

                • Login

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