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. CUrl to QNetworkRequest
Forum Updated to NodeBB v4.3 + New Features

CUrl to QNetworkRequest

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 1.7k 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.
  • N Offline
    N Offline
    neosettler
    wrote on last edited by neosettler
    #1

    Greetings,

    Could anyone help out converting :

    curl -i
    -u USERNAME
    -H 'Content-type: application/json'
    -d '{"secret": "YOUR_APP_KEY"}'
    https://mysite.com/{YOUR_APPID}/tokens

    to a QNetworkRequest?

    Here is what a have so far:

    QUrl l_url(QString("https:// mysite.com/%1/tokens").arg(APP_ID));
    QNetworkRequest l_request(l_url);
    l_request.setRawHeader("u", "mymail@gmail.com");
    QString l_secret = "{\"secret\": \"" + QByteArray(API_SECRET) + "\"}";
    l_request.setRawHeader("d", l_secret.toUtf8());
    l_request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
    
        QNetworkReply *l_reply = m_QNetworkAccessManager->get(l_request);
    connect(l_reply, SIGNAL(finished()), this, SLOT(OnNetworkDataReceived()));
    

    I’m receiving error 401 type so clearly, I’m not getting the expected json data.

    Thank you,

    1 Reply Last reply
    0
    • eyllanescE Offline
      eyllanescE Offline
      eyllanesc
      wrote on last edited by eyllanesc
      #2

      See the curl help:

      curl -h
      

      Output

      ...
       -d, --data <data>   HTTP POST data
      ...
      

      So you shouldn't use get but post:

      QUrl l_url(QString("https:// mysite.com/%1/tokens").arg(APP_ID));
      l_url.setUserName("USERNAME");
      
      QNetworkRequest l_request(l_url);
      l_request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
      
      QJsonObject obj;
      obj["secret"] = QByteArray(API_SECRET);
      QJsonDocument doc(obj);
      QByteArray data = doc.toJson();
      
      
      QNetworkReply *l_reply = m_QNetworkAccessManager->post(l_request, data);
      

      If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

      1 Reply Last reply
      4
      • N Offline
        N Offline
        neosettler
        wrote on last edited by
        #3

        Thank you for your input eyllanesc,

        I forgot to mentioned that this is supposed to be an HTTP Basic Authentication in order to create a user authentication token.

        The response body should looks like this:

        {
        "token": "AUTHENTICATION_TOKEN",
        "refreshToken": "REFRESH_TOKEN"
        }

        With your suggestion the server replied: Internal Server Error

        Are we sure it's a post?

        1 Reply Last reply
        0
        • B Offline
          B Offline
          Bonnie
          wrote on last edited by Bonnie
          #4

          @neosettler
          I don't think "u" / "d" would be the correct header name.
          Add -v to you curl command to print the request header.
          And you should know the method and the header names from that.
          [ADDED]
          I've done some tests myself, it seems the -u option will encode the username:password to base64 and create a Basic Auth header.
          So the password is also needed.
          I think you can:

          1. Try @eyllanesc ‘s code again, but change setUserName("USERNAME") to setUserInfo("USERNAME:PASSWORD").
          2. If the above doesn't work, then set the Authorization header yourself like:
          l_request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
          l_request.setRawHeader("Authorization", "Basic " + QByteArray("USERNAME:PASSWORD").toBase64());
          QNetworkReply *l_reply = m_QNetworkAccessManager->post(l_request, "{\"secret\": \"" + QByteArray(API_SECRET) + "\"}");
          
          1 Reply Last reply
          1
          • N Offline
            N Offline
            neosettler
            wrote on last edited by
            #5

            Thank you guys, all your inputs made this challenge a success:

            @Bonnie I tried your suggestions and here is the working code:

            QUrl l_url(QString("https://mysite.com/%1/tokens").arg(API_ID));
            QJsonObject l_secret;
            l_secret["secret"] = API_SECRET;
            QJsonDocument l_document(l_secret);
            QByteArray l_data = l_document.toJson();
            QNetworkRequest l_request(l_url);
            l_request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
            l_request.setRawHeader("Authorization", "Basic " + QByteArray("username:password").toBase64());
            QNetworkReply *l_reply = m_QNetworkAccessManager->post(l_request, l_data);
            connect(l_reply, SIGNAL(finished()), this, SLOT(OnNetworkDataReceived()));
            

            This is the way,

            Thank you so much!
            Cheers,

            1 Reply Last reply
            0
            • N Offline
              N Offline
              neosettler
              wrote on last edited by neosettler
              #6

              I thought I was getting the hang of it but I'm not out of the woods. It seems simple enough but the documentation is rather confusing:
              https://quixel.github.io/megascans-api-docs/downloading-assets/

              This is an attempt at the next step after getting the AccessToken... and I'm not quite sure how to integrate the following into the url:

              POST /downloads
              {
              "asset": "asset id"
              }

              here is what I've got so far:

              QString l_url = QString("http://downloadf.megascans.se/download/%1?url=https://megascans.se/v1/downloads").arg(l_id);
              
              QJsonObject l_object;
              l_object["asset"] = l_id;
              QJsonDocument l_document(l_object);
              QByteArray l_data = l_document.toJson();
              
              QNetworkRequest l_request(l_url);
              QString l_header = "Bearer " + m_AccessToken;
              l_request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
              l_request.setRawHeader("authorization", l_header.toLocal8Bit());
              QNetworkReply *l_reply = m_QNetworkAccessManager->post(l_request, l_data);
              

              server replied: Method Not Allowed.

              I'm running out of ideas. Any help would be appreciated.
              Thank you,

              B 1 Reply Last reply
              0
              • N neosettler

                I thought I was getting the hang of it but I'm not out of the woods. It seems simple enough but the documentation is rather confusing:
                https://quixel.github.io/megascans-api-docs/downloading-assets/

                This is an attempt at the next step after getting the AccessToken... and I'm not quite sure how to integrate the following into the url:

                POST /downloads
                {
                "asset": "asset id"
                }

                here is what I've got so far:

                QString l_url = QString("http://downloadf.megascans.se/download/%1?url=https://megascans.se/v1/downloads").arg(l_id);
                
                QJsonObject l_object;
                l_object["asset"] = l_id;
                QJsonDocument l_document(l_object);
                QByteArray l_data = l_document.toJson();
                
                QNetworkRequest l_request(l_url);
                QString l_header = "Bearer " + m_AccessToken;
                l_request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
                l_request.setRawHeader("authorization", l_header.toLocal8Bit());
                QNetworkReply *l_reply = m_QNetworkAccessManager->post(l_request, l_data);
                

                server replied: Method Not Allowed.

                I'm running out of ideas. Any help would be appreciated.
                Thank you,

                B Offline
                B Offline
                Bonnie
                wrote on last edited by Bonnie
                #7

                @neosettler From my understanding, you're messing up two steps.
                Step 1: post

                QString l_url = QString("https://megascans.se/v1/downloads")
                ...
                QNetworkReply *l_reply = m_QNetworkAccessManager->post(l_request, l_data);
                

                You should read "id" from the response as DOWNLOAD_ID.

                Step 2: get

                QString l_url = QString("http://downloadf.megascans.se/download/%1?url=https%3A%2F%2Fmegascans.se%2Fv1%2Fdownloads").arg(DOWNLOAD_ID);
                ...
                QNetworkReply *l_reply = m_QNetworkAccessManager->get(l_request);
                

                The response should be a binary zip file.

                1 Reply Last reply
                1
                • N Offline
                  N Offline
                  neosettler
                  wrote on last edited by neosettler
                  #8

                  Yet another leap forward, thank you Bonnie.

                  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