problem in sending a json file to url
-
wrote on 8 Oct 2020, 14:10 last edited by JoeCFD 10 Aug 2020, 14:12
from command line
curl -u username:password -d @test.json http://.../
works fine. The return is
{
"OK" : true,
"Message" : "Configured Successfully"
}test.json:
{
"General": {
"Name": "cat"
}
}Qt code is as follows
QJsonObject name_obj;
name_obj.insert( "Name", "cat" );QJsonObject main_obj; main_obj.insert( "General", name_obj ); QJsonDocument json_document; json_document.setObject( main_obj ); QUrl url = QUrl( "http://***.***.***.***/" ); QString credentials( "username:password" ); QByteArray data = credentials.toLocal8Bit().toBase64(); QString auth = "Basic " + data; QNetworkRequest json_request( url ); //json_request.setHeader( QNetworkRequest::ContentTypeHeader, "application/json" ); json_request.setHeader( QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded" ); json_request.setRawHeader( "Authorization", auth.toLocal8Bit() );
QNetworkAccessManager manager;
auto reply = manager.post( json_request, json_document.toJson() );
std::cout << " error " << qPrintable( reply->errorString() ) << std::endl;
===>error Unknown errorWhat is wrong? I also wrote out json_document and made sure its contents is same as test.json
-
from command line
curl -u username:password -d @test.json http://.../
works fine. The return is
{
"OK" : true,
"Message" : "Configured Successfully"
}test.json:
{
"General": {
"Name": "cat"
}
}Qt code is as follows
QJsonObject name_obj;
name_obj.insert( "Name", "cat" );QJsonObject main_obj; main_obj.insert( "General", name_obj ); QJsonDocument json_document; json_document.setObject( main_obj ); QUrl url = QUrl( "http://***.***.***.***/" ); QString credentials( "username:password" ); QByteArray data = credentials.toLocal8Bit().toBase64(); QString auth = "Basic " + data; QNetworkRequest json_request( url ); //json_request.setHeader( QNetworkRequest::ContentTypeHeader, "application/json" ); json_request.setHeader( QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded" ); json_request.setRawHeader( "Authorization", auth.toLocal8Bit() );
QNetworkAccessManager manager;
auto reply = manager.post( json_request, json_document.toJson() );
std::cout << " error " << qPrintable( reply->errorString() ) << std::endl;
===>error Unknown errorWhat is wrong? I also wrote out json_document and made sure its contents is same as test.json
-
from command line
curl -u username:password -d @test.json http://.../
works fine. The return is
{
"OK" : true,
"Message" : "Configured Successfully"
}test.json:
{
"General": {
"Name": "cat"
}
}Qt code is as follows
QJsonObject name_obj;
name_obj.insert( "Name", "cat" );QJsonObject main_obj; main_obj.insert( "General", name_obj ); QJsonDocument json_document; json_document.setObject( main_obj ); QUrl url = QUrl( "http://***.***.***.***/" ); QString credentials( "username:password" ); QByteArray data = credentials.toLocal8Bit().toBase64(); QString auth = "Basic " + data; QNetworkRequest json_request( url ); //json_request.setHeader( QNetworkRequest::ContentTypeHeader, "application/json" ); json_request.setHeader( QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded" ); json_request.setRawHeader( "Authorization", auth.toLocal8Bit() );
QNetworkAccessManager manager;
auto reply = manager.post( json_request, json_document.toJson() );
std::cout << " error " << qPrintable( reply->errorString() ) << std::endl;
===>error Unknown errorWhat is wrong? I also wrote out json_document and made sure its contents is same as test.json
wrote on 8 Oct 2020, 14:29 last edited by@JoeCFD said in problem in sending a json file to url:
What is wrong? I also wrote out json_document and made sure its contents is same as test.json
You have to wait until
post
request processing is done!
==> see QNetworkReplySomething like this:
QNetworkAccessManager manager; auto reply = manager.post( json_request, json_document.toJson() ); connect(reply, &QNetworkReply::finished, this, [reply]() { qDebug() << "Done" << qPrintable( reply->errorString() ); });
-
@JoeCFD said in problem in sending a json file to url:
What is wrong? I also wrote out json_document and made sure its contents is same as test.json
You have to wait until
post
request processing is done!
==> see QNetworkReplySomething like this:
QNetworkAccessManager manager; auto reply = manager.post( json_request, json_document.toJson() ); connect(reply, &QNetworkReply::finished, this, [reply]() { qDebug() << "Done" << qPrintable( reply->errorString() ); });
wrote on 8 Oct 2020, 14:31 last edited by@KroMignon Thanks for your reply. I will check that out.
-
@JoeCFD said in problem in sending a json file to url:
What is wrong? I also wrote out json_document and made sure its contents is same as test.json
You have to wait until
post
request processing is done!
==> see QNetworkReplySomething like this:
QNetworkAccessManager manager; auto reply = manager.post( json_request, json_document.toJson() ); connect(reply, &QNetworkReply::finished, this, [reply]() { qDebug() << "Done" << qPrintable( reply->errorString() ); });
wrote on 8 Oct 2020, 16:39 last edited byThis post is deleted! -
wrote on 8 Oct 2020, 16:50 last edited by
added: if ( m_reply->isRunning() ) { connect( m_reply, &QNetworkReply::finished, this, &ClassName::postFinished ); } else { postFinished(); }
void ClassName::postFinished()
{
std::cout << " error " << qPrintable( m_reply->errorString() ) << std::endl;
}the output in slot postFinished()
Unknown error -
added: if ( m_reply->isRunning() ) { connect( m_reply, &QNetworkReply::finished, this, &ClassName::postFinished ); } else { postFinished(); }
void ClassName::postFinished()
{
std::cout << " error " << qPrintable( m_reply->errorString() ) << std::endl;
}the output in slot postFinished()
Unknown errorwrote on 8 Oct 2020, 16:58 last edited by JonB 10 Aug 2020, 17:04@JoeCFD
Why are you expecting anything inerrorString()
? How do you know any error is occurring?If I wanted to know about possible errors I would slot onto https://doc.qt.io/qt-5/qnetworkreply.html#errorOccurred.
-
@JoeCFD
Why are you expecting anything inerrorString()
? How do you know any error is occurring?If I wanted to know about possible errors I would slot onto https://doc.qt.io/qt-5/qnetworkreply.html#errorOccurred.
-
@JonB The json file is supposed to change something in the URL. The data was not changed before. It is working now. The errorString() should return empty string if there is no error.
wrote on 8 Oct 2020, 17:14 last edited by JonB 10 Aug 2020, 17:16@JoeCFD said in problem in sending a json file to url:
The errorString() should return empty string if there is no error.
Could you explain that? And what value was https://doc.qt.io/qt-5/qnetworkreply.html#error?
-
@JoeCFD said in problem in sending a json file to url:
The errorString() should return empty string if there is no error.
Could you explain that? And what value was https://doc.qt.io/qt-5/qnetworkreply.html#error?
-
@JonB this one is 0. It is right. The code did not work before. I was checking error message from errorString(). This func should return something like no error or empty. Unknown error is not clear.
wrote on 8 Oct 2020, 17:28 last edited by JonB 10 Aug 2020, 17:32@JoeCFD
:) You don't like a Qt error string, I am not responsible for what it prints and I don't see the docs stating it should return your wish! Earlier today, I was answering some post where the OP didn't like the "no error" text either, was that you? Maybe I'm going senile..., never mind :) I suggest you always check an error number if it is available rather than a string, if you are developing code. But I'm glad your code is working now. -
@JoeCFD
:) You don't like a Qt error string, I am not responsible for what it prints and I don't see the docs stating it should return your wish! Earlier today, I was answering some post where the OP didn't like the "no error" text either, was that you? Maybe I'm going senile..., never mind :) I suggest you always check an error number if it is available rather than a string, if you are developing code. But I'm glad your code is working now. -
added: if ( m_reply->isRunning() ) { connect( m_reply, &QNetworkReply::finished, this, &ClassName::postFinished ); } else { postFinished(); }
void ClassName::postFinished()
{
std::cout << " error " << qPrintable( m_reply->errorString() ) << std::endl;
}the output in slot postFinished()
Unknown errorwrote on 9 Oct 2020, 05:41 last edited by@JoeCFD said in problem in sending a json file to url:
void ClassName::postFinished()
{
std::cout << " error " << qPrintable( m_reply->errorString() ) << std::endl;
}
the output in slot postFinished()
Unknown errorThis was only an example to explain how-to get in touch when request is done!
I just rewrite your code to made it work.
A more logical process would be:connect(reply, &QNetworkReply::finished, this, [reply]() { if (reply->error() == QNetworkReply::NoError) { QByteArray data = reply->readAll(); QDebug() << "Success:" << data; } else qDebug() << "Failure: " << qPrintable( reply->errorString() ); });
1/13