I am trying to send http POST request. Here is code snippets:
const QUrl URL("https://httpbin.org/post");
QNetworkRequest req(URL);
req.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
QUrlQuery urlQuery;
urlQuery.addQueryItem ("username", username);
urlQuery.addQueryItem ("password", password);
QUrl params;
params.setQuery (urlQuery);
mNetReply = mNetMan->post(req, params.toEncoded());
and here is the response output:
QJsonObject({"args":{},"data":"","files":{},"form":{"?username":"xyz","password":"xyz"},"headers":{"Accept-Encoding":"gzip, deflate","Accept-Language":"en-US,*","Connection":"close","Content-Length":"28","Content-Type":"application/x-www-form-urlencoded","Host":"httpbin.org","User-Agent":"Mozilla/5.0"},"json":null,"origin":"*.*.*.*","url":"https://httpbin.org/post"})
My problem is , qt automatically adding a ? mark in first queryItem.
For what it's worth still, because an answer was not given:
Don't use the conversion to a QUrl, using the QUrlQuery directly solved the problem for me.
urlQuery.toString(QUrl::FullyEncoded).toUtf8()
in stead of
params.toEncoded()
Total snippet updated:
const QUrl URL("https://httpbin.org/post");
QNetworkRequest req(URL);
req.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
QUrlQuery urlQuery;
urlQuery.addQueryItem ("username", username);
urlQuery.addQueryItem ("password", password);
mNetReply = mNetMan->post(req, urlQuery.toString(QUrl::FullyEncoded).toUtf8());
Related
I'm developing a Qt application and I want to use Google authentication for it. I created a Google API as explained in the following link: https://blog.qt.io/blog/2017/01/25/connecting-qt-application-google-services-using-oauth-2-0/ but I have a problem with it. It doesn't work in many cases and I get ProtocolInvalidOperationError(302) error for https://accounts.google.com/o/oauth2/token request URL in
QOAuthHttpServerReplyHandler::networkReplyFinished(QNetworkReply *reply)
method of Qt class.
Note that I override QOAuthHttpServerReplyHandler::networkReplyFinished(QNetworkReply *reply) to get this error, because it doesn't emit any signal in this case, and the return value for reply->readAll() is as below:
{
"error": "invalid_grant",
"error_description": "Malformed auth code."
}
My Login.cpp code is something as below:
Login::Login() {
google = new QOAuth2AuthorizationCodeFlow;
google->setScope("email");
google->setAuthorizationUrl("https://accounts.google.com/o/oauth2/auth");
google->setClientIdentifier(Utility::decrypt(encryptedClientId));
google->setAccessTokenUrl("https://accounts.google.com/o/oauth2/token");
google->setClientIdentifierSharedKey(Utility::decrypt(encryptedClientSecret));
connect(google, &QOAuth2AuthorizationCodeFlow::authorizeWithBrowser,
&QDesktopServices::openUrl);
connect(google,&QOAuth2AuthorizationCodeFlow::authorizationCallbackReceived,[=](const QVariantMap data){
QString code(data["code"].toString());
if(!code2.isEmpty())
{
const QUrl redirectUri= "http://localhost:56413/cb";
QJsonObject postdata;
postdata.insert("code",code);
postdata.insert("client_id", Utility::decrypt(encryptedClientId));
postdata.insert("client_secret", Utility::decrypt(encryptedClientSecret));
postdata.insert("redirect_uri", redirectUri.toString());
postdata.insert("grant_type","authorization_code");
QString serviceURL = "oauth2/v4/token";
NetworkManager::GetInstance()->Post(postdata,serviceURL,"https://www.googleapis.com/",[=](int statusCode,int resultnumber, QJsonObject obj){
if (statusCode >= 200 &&
statusCode < 300)
{
// it's ok, do nothing
}
else {
//show error
}
});
}
});
}
void Login::googleLoginButtonPressed() {
int googlePort = 56413;
if(replyHandler == nullptr)
replyHandler = new QOAuthHttpServerReplyHandlerArio(googlePort, this);
google->setReplyHandler(replyHandler);
QObject::connect(replyHandler, &QOAuthHttpServerReplyHandler::tokensReceived, [=](const QVariantMap &map) {
googleToken = map["id_token"].toString();
connect(google, &QOAuth2AuthorizationCodeFlow::granted, [=]() {
auto reply = google->get(QUrl("https://www.googleapis.com/plus/v1/people/me"));
connect_reply = connect(reply, &QNetworkReply::finished, [=]() {
int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
if (statusCode >= 200 &&
statusCode < 300)
{
//NOW register or login the user with email
QJsonDocument jsonResponse = QJsonDocument::fromJson(reply->readAll().data());
email = jsonResponse.object().value("emails").toArray()[0].toObject().value("value").toString();
reply->deleteLater();
}
else {
//error
}
});
});
});
google->grant();
}
what's the problem?
Thanks for your help.
We have posted a lengthy document describing how to authenticate with Google SSO and Qt and this is one of the problems we discuss. I suspect the reason is that the login code returned by Google is URL-encoded, and Qt does not decode it automatically for you. So before you set your replyHandler, you need to invoke setModifyParametersFunction to decode it, in the middle of the flow.
google->setModifyParametersFunction([](QAbstractOAuth::Stage stage, QVariantMap* parameters) {
// Percent-decode the "code" parameter so Google can match it
if (stage == QAbstractOAuth::Stage::RequestingAccessToken) {
QByteArray code = parameters->value("code").toByteArray();
(*parameters)["code"] = QUrl::fromPercentEncoding(code);
}
});
I'm trying to insert an event to google calendar using Qt C++.
Before inserting events, I am able to clear the primary calendar using these lines
const QUrl clearPrimaryUrl("https://www.googleapis.com/calendar/v3/calendars/primary/clear");
void GoogleWrapper::clearPrimaryCalendar()
{
QNetworkReply *reply = google.post(clearPrimaryUrl);
connect(reply, &QNetworkReply::finished, [=]()
{
reply->deleteLater();
if(reply->error() != QNetworkReply::NoError)
{
qCritical() << "Google error:" << reply->errorString();
return;
}
emit primaryCalendarCleared();
});
}
google is an QOAuth2AuthorizationCodeFlow object.
Now to insert events, I use these lines:
const QUrl insertEventUrl("https://www.googleapis.com/calendar/v3/calendars/primary/events");
void GoogleWrapper::insertEvent(const QByteArray &eventData)
{
qDebug() << eventData;
QNetworkReply *reply = google.post(insertEventUrl, eventData);
connect(reply, &QNetworkReply::finished, [=]()
{
reply->deleteLater();
if(reply->error() != QNetworkReply::NoError)
{
qCritical() << "Google error:" << reply->errorString();
return;
}
emit eventInserted();
});
}
But the output I received from qCritical was always Error transferring https://www.googleapis.com/calendar/v3/calendars/primary/events - server replied: Bad Request.
I tried output the eventData to a file and copy paste it to Google's try this API and the below json data works perfectly
{
"description": "some teacher name",
"end": {
"dateTime": "2018-12-19T11:15:00Z"
},
"location": "Room",
"start": {
"dateTime": "2018-12-19T09:30:00Z"
},
"summary": "Subject Name"
}
Anyone have any idea what I did wrong?
The issue is solved thanks to #thuga's comment.
My code was missing the line
google.setContentType(QAbstractOAuth2::ContentType::Json);
I am writing a simple qt network application. I try to read the data from the QNetworkReply but it seems that the finished signal never emit. What happens?
QByteArray utils::Login(QString account)
{
QNetworkAccessManager* manager = new QNetworkAccessManager();
QNetworkRequest* request = new QNetworkRequest();
request->setUrl(QUrl(Urls::loginUrl));
request->setRawHeader("Host", "10.136.2.5");
request->setRawHeader("Referer", "http://10.136.2.5/jnuweb/");
request->setRawHeader("Content-Type", "application/json; charset=utf-8");
request->setRawHeader("Connection", "keep-alive");
request->setRawHeader("X-Requested-With", "XMLHttpRequest");
request->setRawHeader("Accept", "*/*");
request->setRawHeader("Accept-Encoding", "deflate");
QJsonObject* requestContent = new QJsonObject();
requestContent->insert("user", QJsonValue(account));
requestContent->insert("password", QJsonValue(Urls::initPassword));
QNetworkReply* reply = manager-> post(*request, QJsonDocument(*requestContent).toJson(QJsonDocument::Compact));
QObject::connect(reply, &QNetworkReply::finished, [=]()
{
QList<QPair<QByteArray, QByteArray>> responses = reply -> rawHeaderPairs();
qDebug() << responses;
});
}
I'm trying to pull JSON data from a URL using the Qt framework, and I've run into some issues with getting a return from the get request. I've seen multiple questions asked regarding my issue, but none of the given solutions has solved my problem.
So I have a button that, once clicked, should execute the get request. Here's my code.
//When button is pressed
void Test1::onClickCapture()
{
qDebug() << "Capture Clicked!!";
toPopulate();
}
//Code to execute the connection
void Test1::toPopulate() {
qDebug() << "Populating!";
QNetworkAccessManager* manager = new QNetworkAccessManager(this);
QNetworkRequest request;
QUrl url("https://jsonplaceholder.typicode.com/posts/1");
request.setUrl(url);
QNetworkReply *reply = manager->get(request);
connect(reply, &QNetworkReply::readyRead, this, &Test1::onResult);
}
//And finally, my onResult slot
void Test1::onResult() {
reply->deleteLater();
if (reply->error() != QNetworkReply::NoError) {
return;
}
qDebug() << "Response!";
}
Every time I debug the code, it never gets to my onResult slot.
I also get this error, which may be relevant.
QObject::connect: Cannot connect (null)::aboutToQuit() to QNativeWifiEngine::closeHandle()
I have modified your code. It's working fine for me:
//Code to execute the connection
void MainWindow::toPopulate() {
qDebug() << "Populating!";
QNetworkAccessManager* manager = new QNetworkAccessManager(this);
QNetworkRequest request;
QUrl url("https://jsonplaceholder.typicode.com/posts/1");
request.setUrl(url);
QNetworkReply *reply = manager->get(request);
connect(reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(downloading(qint64,qint64)));
connect(reply, SIGNAL(finished()), this, SLOT(onResult()));
}
void MainWindow::downloading(qint64 bytesReceived, qint64 bytesTotal) {
qDebug() << "Downloading " << bytesReceived/bytesTotal*100 << " %.";
}
//And finally, my onResult slot
void MainWindow::onResult() {
QNetworkReply* reply = qobject_cast<QNetworkReply*> (QObject::sender());
if (reply->error() != QNetworkReply::NoError) {
qDebug() << "Error downloading. " << reply->errorString();
return;
}
reply->deleteLater();
qDebug() << "Response! " << reply->readAll();
}
Result:
Populating!
Downloading -29200 %.
Downloading 100 %.
Response! "{\n \"userId\": 1,\n \"id\": 1,\n \"title\": \"sunt aut facere repellat provident occaecati excepturi optio reprehenderit\",\n \"body\": \"quia et suscipit\\nsuscipit recusandae consequuntur expedita et cum\\nreprehenderit molestiae ut ut quas totam\\nnostrum rerum est autem sunt rem eveniet architecto\"\n}"
I am developing a Qt/C++ program which encapsulates an HTML5/JQuery web app.
I used to make Ajax requests to read files from a server. But now, I would like Qt to read a file from the local disk and send its content to my web app.
I think I need Qt to catch Ajax requests from the web app and return the file content as the Ajax request result.
The problem is I don't know how to do. For now, I've not found anything about that on google.
Any help is welcome!
I finally found how to do it. I overrode QNetworkAccessManager.
MyQNetworkAccessManager .h:
class MyQNetworkAccessManager : public QNetworkAccessManager
{
Q_OBJECT
protected:
virtual QNetworkReply * createRequest(Operation op, const QNetworkRequest & req, QIODevice * outgoingData = 0);
};
MyQNetworkAccessManager.cpp:
QNetworkReply * MyQNetworkAccessManager::createRequest(Operation op, const QNetworkRequest & req, QIODevice * outgoingData) {
QUrl url = req.url();
QString path = url.path();
if (op == QNetworkAccessManager::GetOperation && path.endsWith("xml")) {
QUrl newUrl;
if(path.endsWith("..")) {
newUrl.setUrl("...");
}
else if(path.endsWith("...")) {
newUrl.setUrl("...");
}
else {
newUrl = url;
}
return QNetworkAccessManager::createRequest(QNetworkAccessManager::GetOperation, QNetworkRequest(newUrl));
}
else
{
return QNetworkAccessManager::createRequest(op, req, outgoingData);
}
}
MainWindow.cpp:
// ....
QWebView *qWebView = new QWebView();
QWebPage *page = qWebView->page();
MyQNetworkAccessManager *networkManager = new MyQNetworkAccessManager();
page->setNetworkAccessManager(networkManager);
qWebView->setPage(page);
qWebView->load(QUrl("..."));
// ....