Mbded
Mbded

Reputation: 1916

Request JSON in Qt

Firstly before I will start post, I whant to tell that I read this topic

Correct format for HTTP POST using QNetworkRequest

I using this idea but for me is does not working. I also read a few other topic but all time I do not have solve. May be therefore because I somethig missed (for sure).

Now I will show what request is work with html file

<html>
<body>
<form method="POST" action="http://192.168.1.108/acces.cgi">
<input type="text" name="json"
value='{"method":{"ok":"get","number":1}}' size="100">
<input type="submit">
</form>
</body>
</html>

After send this request I get answer in browser. So request JSON is correct for sure.

Now I whant try send this same request but using Qt so I have written below code but I only get warring

qt.network.ssl: QSslSocket: cannot resolve SSlv2_client_method
qt.network.ssl: QSslSocket: cannot resolve SSlv2_server_method

And My Code

QUrl url("http://192.168.1.108/acces.cgi"); // request to local host
QNetworkRequest *request = new QNetworkRequest(url);
request>setHeader(QNetworkRequest::ContentTypeHeader,"application/json");
manager = new QNetworkAccessManager();
QObject::connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(HandledDataFromNetwork(QNetworkReply*)));

// prepare JSON string
QByteArray array= "{\"method\":{\"ok\":\"get\",\"number\":\"1\"}}";

// and Send
reply = manager->post(*request, array);

// next I wait for signal and try recive in slot
void MyClass::HandledDataFromNetwork(QNetworkReply *reply)
{
    qDebug() << "content" << reply->readAll();
}

But expect warrning I not get nothing more.

Ps. Sorry for that how it looks code in Qt I do not can correct preper better

EDIT: SOLVED !!!

Answer is using QUrlQuery class. So code should be looks like below:

QUrl url("http://192.168.1.109/ask.cgi");
QUrlQuery query;
query.setQuery("json={\"ctrl\":{\"c":\"gd\",\"i\":6}}");
url.setQuery(query);
QNetworkRequest req(url);

QNetworkAccessManager test;

QEventLoop loop;
connect(&test, SIGNAL(finished(QNetworkReply*)), &loop, SLOT(quit()));
QNetworkReply * reply = test.get(req);
loop.exec();

:)

Upvotes: 0

Views: 1544

Answers (1)

rm5248
rm5248

Reputation: 2645

This error:

qt.network.ssl: QSslSocket: cannot resolve SSlv2_client_method

is probably because QT can't find OpenSSL. If you're on Windows, you'll have to get some OpenSSL binaries and put them in the same folder as your executable. If you're on Linux, install OpenSSL through your package manager (apt-get install openssl for Debian-based systems).

Also, if you're sending JSON data, look at QTs JSON support, it will make it much easier to send/parse JSON data.

Upvotes: 0

Related Questions