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. How to send PUT or POST requests with Request Body to Google API
Forum Updated to NodeBB v4.3 + New Features

How to send PUT or POST requests with Request Body to Google API

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 423 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.
  • H Offline
    H Offline
    Hristo Konstantinov
    wrote on last edited by
    #1

    I'm writing a small desktop app that consumes the Google calendar API. I've successfully authenticated via QOAuth2AuthorizationCodeFlow and I've managed to get the list of calendars and events. Now I want to be able to edit the calendar events or create new ones. As stated in the documentation, the request should have scope (already set with the authentication itself), parameters AND request body. However the overloaded functions I see on QOAuth2AuthorizationCodeFlow::put and QOAuth2AuthorizationCodeFlow::post accept the following arguments:
    (const QUrl &url, const QVariantMap &parameters);
    (const QUrl &url, const QByteArray &data);
    (const QUrl &url, QHttpMultiPart *multiPart);

    I have no idea where to put the request body (which is just a simple json file)? Which functions or classes to use to build the actual request properly, according to the documentation??

    1 Reply Last reply
    0
    • artwawA Offline
      artwawA Offline
      artwaw
      wrote on last edited by
      #2

      I wrote - some years ago - a user manager interfacing with Google Workspace. I do remember using QVariantMap for this purpose.

      For more information please re-read.

      Kind Regards,
      Artur

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

        You mean your body content is already in a file? Then you just read the file to a QByteArray.

        H 1 Reply Last reply
        1
        • B Bonnie

          You mean your body content is already in a file? Then you just read the file to a QByteArray.

          H Offline
          H Offline
          Hristo Konstantinov
          wrote on last edited by
          #4

          @Bonnie yes, but if I use post(const QUrl &url, const QByteArray &data) I have no way to set the parameters and vice versa.

          JonBJ 1 Reply Last reply
          0
          • H Hristo Konstantinov

            @Bonnie yes, but if I use post(const QUrl &url, const QByteArray &data) I have no way to set the parameters and vice versa.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @Hristo-Konstantinov
            Please understand I know nothing about this so cannot answer further! But for "I have no way to set the parameters" is void QAbstractOAuth::setModifyParametersFunction(const QAbstractOAuth::ModifyParametersFunction &modifyParametersFunction)

            Sets the parameter-modification function modifyParametersFunction. This function is used to customize the parameters sent to the server during a specified authorization stage

            what you need, as per https://interest.qt-project.narkive.com/y1uCMfSS/send-post-request-with-qoauth2authorizationcodeflow from 8 years ago!?

            1 Reply Last reply
            0
            • H Offline
              H Offline
              Hristo Konstantinov
              wrote on last edited by Hristo Konstantinov
              #6

              After some digging through the source code of QAbstractOAuth to see which classes are used under the hood I managed to do it this way:

              
              std:: string json = CalendarJsonParser::writeEventQuery(event); // the Request Body - yes, I'm using the jsoncpp library instead of QJsonObject, for some reason
              
              QVariantMap parameters;
              parameters["calendarId"] = QString::fromStdString(calendar_list[calendarIdx].id); //the Parameters
              
              QString urlStr = "https://www.googleapis.com/calendar/v3/calendars/calendarId/events";
              
              QUrl url = QUrl(urlStr);
              
              QUrlQuery query; //the query should store the parameters
              
              //adding the parameters to the query
              for (auto it = parameters.begin(), end = parameters.end(); it != end; ++it){
                  query.addQueryItem(it.key(), it.value().toString());
              }
              
              //adding the query to the url:
              url.setQuery(query);
              
              //creating request from the url
              QNetworkRequest req(url);
              
              QByteArray verb = "POST"; //POST, PUT, use whatever the API defines
              
              //google is my QOAuth2AuthorizationCodeFlow object, so I'm adding the additional token stuff here
              google->prepareRequest(&req, verb, json.data());
              
              //getting the reply pointer:
              auto reply = google->networkAccessManager()->sendCustomRequest(req, verb, json.data());
              
              //doing something with the reply
              connect(reply, &QNetworkReply::finished, this, [=]() { /*implement handling here*/ }); 
              
              B 1 Reply Last reply
              1
              • H Hristo Konstantinov has marked this topic as solved on
              • H Hristo Konstantinov

                After some digging through the source code of QAbstractOAuth to see which classes are used under the hood I managed to do it this way:

                
                std:: string json = CalendarJsonParser::writeEventQuery(event); // the Request Body - yes, I'm using the jsoncpp library instead of QJsonObject, for some reason
                
                QVariantMap parameters;
                parameters["calendarId"] = QString::fromStdString(calendar_list[calendarIdx].id); //the Parameters
                
                QString urlStr = "https://www.googleapis.com/calendar/v3/calendars/calendarId/events";
                
                QUrl url = QUrl(urlStr);
                
                QUrlQuery query; //the query should store the parameters
                
                //adding the parameters to the query
                for (auto it = parameters.begin(), end = parameters.end(); it != end; ++it){
                    query.addQueryItem(it.key(), it.value().toString());
                }
                
                //adding the query to the url:
                url.setQuery(query);
                
                //creating request from the url
                QNetworkRequest req(url);
                
                QByteArray verb = "POST"; //POST, PUT, use whatever the API defines
                
                //google is my QOAuth2AuthorizationCodeFlow object, so I'm adding the additional token stuff here
                google->prepareRequest(&req, verb, json.data());
                
                //getting the reply pointer:
                auto reply = google->networkAccessManager()->sendCustomRequest(req, verb, json.data());
                
                //doing something with the reply
                connect(reply, &QNetworkReply::finished, this, [=]() { /*implement handling here*/ }); 
                
                B Offline
                B Offline
                Bonnie
                wrote on last edited by
                #7

                @Hristo-Konstantinov I think you just need the QUrlQuery part to get the url with parameters, then you could use QOAuth2AuthorizationCodeFlow::post(const QUrl &url, const QByteArray &data).
                They should be the same.

                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