Solved problem in sending a json file to url
-
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
-
@JoeCFD I am using Qt 5.9.5 on Ubuntu.
-
@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() ); });
-
@KroMignon Thanks for your reply. I will check that out.
-
This post is deleted! -
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 -
@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.
-
@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.
-
@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. -
@JonB First, I appreciate your reply. Unknown error is kind of confusing for me. I did not ask you to change the output of errorString(). And I did not post anywhere else except in this topic today.
-
@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() ); });