M
M
Mars362021-03-04 16:14:59
Qt
Mars36, 2021-03-04 16:14:59

How to make a POST request in JSON format in QT?

What the request should look like:

{
   "app": "*",
   "time": "2014-11-07T16:21:02+03:00",
   "requests": [
           {
           "func": "r_genres_list",
           "id": "genres_list"
           }
   ]
}

I can't figure out how to format this block:
"requests": [
           {
           "func": "r_genres_list",
           "id": "genres_list"
           }
   ]

Tried like this:
QUrl url ( "https://requestbin.net/r/bfeldb43" );
    QByteArray reqdata;
    reqdata.append( "&time=" + QDateTime::currentDateTime().toTimeSpec( Qt::OffsetFromUTC ).toString( Qt::ISODate ) );
    reqdata.append( "&app=1" );
    reqdata.append( "&requests= { \"func\": \"r_genres_list\", \"id\":\"genres_list\"}]" );
    QNetworkRequest request( url  );
    request.setRawHeader(  "jdata", "1" );
    manager->post( request, reqdata );

The output turned out
This

6040dd0007750508218082.png

As I understand it, the parameter is not perceived as an array, but in one line. How can I make a request correctly?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ighor July, 2021-03-04
@Mars36

To do this, you'd better create a QJsonObject or QVariantMap, fill it with data.
And then store them in QByteArray using QJsonDocument https://doc.qt.io/qt-5/qjsondocument.html

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question