How to send PUT or POST requests with Request Body to Google API
-
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 ¶meters);
(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??
-
You mean your body content is already in a file? Then you just read the file to a QByteArray.
@Bonnie yes, but if I use post(const QUrl &url, const QByteArray &data) I have no way to set the parameters and vice versa.
-
@Bonnie yes, but if I use post(const QUrl &url, const QByteArray &data) I have no way to set the parameters and vice versa.
@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!?
-
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*/ });
-
-
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*/ });
@Hristo-Konstantinov I think you just need the
QUrlQuery
part to get the url with parameters, then you could useQOAuth2AuthorizationCodeFlow::post(const QUrl &url, const QByteArray &data)
.
They should be the same.