NetworkReply HttpStatusCode QVariant(Invalid) - qt

I'm using the code below to make a http request:
QNetworkReply* ApiRequest::req(QString url, QString method, QByteArray data) {
QByteArray request_method = method.toUtf8();
QNetworkAccessManager* manager = new QNetworkAccessManager();
QNetworkRequest request("http://127.0.0.1:9090" + url);
request.setRawHeader("Content-Type", "application/json");
QNetworkReply* reply = manager->sendCustomRequest(request, request_method, data);
return reply;
}
void ApiRequest::requestConfig()
{
NetworkReply* reply = req("/configs",
"GET",
"");
}
The Remote server did execute the request and reply a 204 code.
I have used wireshark to capture and make sure it had reply a 204 No Content.
However, the output there is QVariant(Invalid), the toInt output is 0.
I tried to change PUT to GET but it still not working.

You are analyzing the state even when the request has not been made so it is valid that the result is null, what you should do is analyze it when the finished signal is emitted:
QNetworkReply* reply = mg->sendCustomRequest(request, "PUT", "....some json....");
connec(reply, &QNetworkReply::finished, [reply](){
qDebug() << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
});
Update:
connec(mg, &QNetworkAccessManager::finished, [](QNetworkReply *reply){
qDebug() << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
});
Update 2:
Is there a way to return that reply when it is finished?
Yes, use a QEventLoop, but in general it is a bad design since you should use signals and slots to notify you of the changes.
QNetworkReply* ApiRequest::req(const QString & url, const QString & method, const QByteArray & data) {
QByteArray request_method = method.toUtf8();
QNetworkAccessManager manager;
QNetworkRequest request("http://127.0.0.1:9090" + url);
request.setRawHeader("Content-Type", "application/json");
QNetworkReply* reply = manager.sendCustomRequest(request, request_method, data);
QEventLoop loop;
connec(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
loop.exec();
return reply;
}
// ...
void ApiRequest::requestConfig()
{
QNetworkReply* reply = req("/configs", "GET", "");
// ...
reply->deleteLater();
}

You can include QtNetworkReply and QEventLoop classes and use signal/slot mechanism.
QNetworkAccessManager *manager = new QNetworkAccessManager(this);
QUrl resource(url);
QNetworkRequest request(resource);
QNetworkReply *reply = manager->get(request);
QEventLoop loop;
connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
loop.exec();
QJsonObject jsonObject = QJsonDocument::fromJson(reply->readAll()).object();

Related

QNetworkReply::RemoteHostClosedError

I was learning the Github API and wanted to create an Issue on github by Qt.
But, when I submit my Issue, the "Debug Console" said
QNetworkReply::RemoteHostClosedError
(I catched error).
I don't know why, please help me!
Code on Github:
void MainWindow::on_pushButton_2_clicked()
{
QString id = ui->ID_Send->text();
QJsonObject JJ;
QJsonDocument jd;
JJ["title"] = ui->Title->text();
JJ["body"] = ui->Comment->toPlainText();
QHttpMultiPart *part = new QHttpMultiPart(QHttpMultiPart::FormDataType);
QHttpPart info;
info.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("text/json"));
jd.setObject(JJ);
info.setBody(jd.toJson());
part->append(info);
QNetworkRequest request;
qDebug()<<"https://api.github.com/repos/MoyuSteve/Comment/issues?client_id="+
client_id+"&client_secret="+client_secret;
request.setUrl(QUrl("https://api.github.com/repos/MoyuSteve/Comment/issues?client_id="+
client_id+"&client_secret="+client_secret));
QNetworkReply* reply = manager->post(request,part);
QObject::connect(reply, &QNetworkReply::finished, [reply, this] () {
if (reply->error() == QNetworkReply::NoError) {
ui->status->setText("ok");
} else {
ui->status->setText("failed");
qDebug()<<reply->error();
}
reply->deleteLater();
});
}
You can take example in this answer or here.
Compared to your code, it sets the header to "application/json".
And it uses a QNetworkAccessManager to send network requests and receive replies.
QUrl url("https://api.github.com/repos/" + owner +"/" + project + "/tags");
qInfo() << url.toString();
QNetworkRequest request(url);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
QNetworkAccessManager nam;
QNetworkReply * reply = nam.get(request);

Why file name is not set when uploaded to google drive?

My Objective
I want to upload a sqlite file to google drive.
Although I have been able to achieve uploading the file to google drive, the file uploaded name is being "Untitled". I tried Performing Multipart Upload with the help of Qt's QHttpMultiPart.
Here is my code
bool File_Control::Upload_File_Multipart(const QString &FileName, const QByteArray &rawData)
{
if(FileName.isEmpty()){
emit setMessage("Error: File Name can not be empty");
return false;
}
if(rawData.size()==0){
emit setMessage("Error: File size can not be zero");
return false;
}
// Prepare MetaDataPart
QHttpPart MetadataPart;
MetadataPart.setRawHeader("Content-Type", "application/json; charset=UTF-8");
QString Body;
Body = "{\n"
+ tr("\"name\": \"%1\"\n").arg(FileName)
+ tr("}");
MetadataPart.setBody(Body.toUtf8());
// Prepare MediaPart
QHttpPart MediaPart;
MediaPart.setRawHeader("Content-Type", "application/octet-stream");
MediaPart.setBody(rawData);
// Now add MetaDataPart and MediaPart together to form multiPart
QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::RelatedType);
multiPart->setBoundary("bound1");
multiPart->append(MetadataPart);
multiPart->append(MediaPart);
// Now Prepare the request
QUrl url("https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart");
QNetworkRequest request(url);
QString AccessTokenBearer = "Bearer " + mAccess_Token; // mAccess_Token holds the access_token for the session
request.setRawHeader("Content-Type","Content-Type: multipart/related; boundary=bound1");
request.setRawHeader("Authorization", AccessTokenBearer.toUtf8());
// Get Reply and connect it to set Value
QNetworkReply *reply = mQNAM.post(request, multiPart); // mQNAM is QNetworkAccessManager
QObject::connect(reply, SIGNAL(uploadProgress(qint64,qint64)),
this, SIGNAL(setValue(qint64,qint64))); // Update in gui
// Set timeout mechanism while waiting for reply finished
bool stop = false;
QTimer timer;
timer.setSingleShot(true);
QObject::connect(reply, &QNetworkReply::uploadProgress, [&](){
timer.stop();
timer.start(TIMEOUT); // Restart timer every time upload progress
});
QObject::connect(&timer, &QTimer::timeout, [&](){
stop = true;
});
timer.start(TIMEOUT);
// Wait till the response is completed
while(!reply->isFinished()){
QCoreApplication::processEvents();
if(stop){
reply->abort();
}
}
// Check reply
if(reply->error() != QNetworkReply::NoError){
if(reply->error() == QNetworkReply::OperationCanceledError){
emit setMessage("Error: Timeout");
}
else{
emit setMessage("Error: "+reply->errorString());
}
delete reply;
return false;
}
else{
QByteArray rawData = reply->readAll();
qDebug()<<"Size of reply "<<rawData.size();
qDebug()<<"Data "<<rawData;
delete reply;
return true;
}
}
Edit 1
Body.utf8 log -> "{\n\"name\": \"db_06_08_2018_16_18_11_979.sqlite\"\n}"
I finally found the mistake. It was nothing but a typo in preparing the request
Before:
request.setRawHeader("Content-Type","Content-Type: multipart/related; boundary=bound1");
After:
request.setRawHeader("Content-Type","multipart/related; boundary=bound1");
This solved the issue.

QNetworkAccessManager: how to make sure download is finished?

If I do:
QNetworkRequest newRequest(url);
newRequest.setUrl(url);
QNetworkReply *reply = networkManager->get(newRequest);
connect(reply, SIGNAL(finished()), this, SLOT(onRetrievedDownLoadInfo()));
and
void myClass::onRetrievedDownLoadInfo()
{
QNetworkReply *reply = qobject_cast<QNetworkReply*>(QObject::sender());
if (!reply)
{
ui->upgradeLog->appendPlainText("Download failed, invalid context");
return;
}
//Remeber to delete the reply
//---------------------------
reply->deleteLater();
if (reply->error() != QNetworkReply::NoError)
{
ui->upgradeLog->appendPlainText("Download failed, invalid context");
return;
}
reply->readAll();
...
}
Can I say that when the program reaches line
reply->readAll();
All the content replied by the server is successfully received?
Is it possible the reply->readAll(); only returns part of the reply due to network failure?
The error is here:
connect(reply, SIGNAL(finished()), this, SLOT(onRetrievedDownLoadInfo()));
you are not doing it in the correct way. Use a QNetworkAccessManager to check the network is working:
QNetworkConfigurationManager manager;
req.setConfiguration(manager.defaultConfiguration());
connect(&req, SIGNAL(networkAccessibleChanged(QNetworkAccessManager::NetworkAccessibility)), this, SLOT(networkAccessibleChanged(QNetworkAccessManager::NetworkAccessibility)));

QNetworkAccessManager returning empty results

I am struggling with qnetworkaccessmanager for quite sometime. I googled a lot, but I donot find a solution for this.
I am creating a client using qaccessmanager to talk with a rest server. QNetworkReply is not returning any results. The server is working properly but the client is not returning results. On top of that the server gets called 3 times and sometimes the server is crashing. Hope some one can figure out what is going wrong. I am attaching the client code.
I tried different approches like connecting finished signal of networkaccessmanager, qnetworkreply e.t.c. But all of them ends up in giving the same error "Connection Closed" or the readAll bytearray being empty.
void RestClientCore::ConnectToServer()
{
m_NetworkManager = new QNetworkAccessManager(this);
QUrl url("http://localhost");
url.setPort(5432);
QByteArray postData;
postData.append("/?userid=user");
postData.append("&site=site");
QNetworkReply *reply = m_NetworkManager->post(request,postData);
connect(reply, SIGNAL(readyRead()),this, SLOT(slotReadyRead()));
connect(reply, SIGNAL(finished()), this, SLOT(onRequestCompleted()));
}
void RestClientCore::onRequestCompleted() {
QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
if(reply->error())
{
qDebug() <<reply->bytesAvailable() << reply->errorString();
}
else
{
qDebug() << reply->readAll();
}
reply->deleteLater();
}
void RestClientCore::slotReadyRead()
{
QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
qDebug() << reply->readAll();
}
Thanks in advance
Regards
Rejo

how can I read the data sent from the server in a variable of type QNetWorkReply?

I have used this code to sent the user name and a password to the server using POST Method.
This returns me a reply,So how can I access the reply variable in the way that i can read the data sent from the server back to me ??
Used Code:
void MainWindow::post(QString name, QString password)
{
QUrl serviceUrl = QUrl("http://lascivio.co/mobile/post.php");
QByteArray postData;
QString s = "param1="+name+"&";
postData.append(s);
s = "param2=" +password;
postData.append(s);
QNetworkAccessManager *networkManager = new QNetworkAccessManager(this);
connect(networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(serviceRequestFinished(QNetworkReply*)));
QNetworkRequest request;
request.setUrl(serviceUrl);
QNetworkReply* reply = networkManager->post(request, postData);
}
void MainWindow::serviceRequestFinished(QNetworkReply* reply)
{
//????????????
}
QNetworkReply is a QIODevice, so you can read it the same way you would read a file. But you have to destroy the QNetworkReply and check for error too in that slot.
For example, in the simplest case (without HTTP redirection):
void MainWindow::serviceRequestFinished(QNetworkReply* reply)
{
// At the end of that slot, we won't need it anymore
reply->deleteLater();
if(reply->error() == QNetworkReply::NoError) {
QByteArray data = reply->readAll();
// do something with data
...
} else {
// Handle the error
...
}
}
You should probably declare the QNetworkAccessManager variable as a member of your class instead of creating a new one for each request.

Resources