Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

CUrl to QNetworkRequest



  • 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,



  • 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);
    


  • 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?



  • @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) + "\"}");
    


  • 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,



  • 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,



  • @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.



  • Yet another leap forward, thank you Bonnie.


Log in to reply