QNetworkAccessManager send PUT request with JSON

42 Views Asked by At

I want to send a put request with JSON body, but with QNetworkAccessManager doesn't have the option to send a request with JSON so I converted it to QByteArray, the structure of the body, when converted to QByteArray, is not a JSON format so that I received an Error with

"Error downloading https://example.api.com/something/DataSet('123456780XYZ') - server replied: Forbidden"

The JSON format I want is like this:

{
    "d": {
        "Duration": "0.000",
        "Id": "123456780XYZ",
        "LoadCumulLimit": "0.000",
        "Notes": "send from postman in Json format"
    }
}

Here is what did I do

    QJsonObject data;
    data["Duration"] = workplace->data["duration"];
    data["Id"] = id;
    data["LoadCumulLimit"] = workplace->data["load_cumul_limit"];
    data["Notes"] = workplace->dataText["Notes"];

    QJsonObject newData;
    newData["d"] = data;
    QJsonDocument doc(newData);

    qDebug() << url;
    QByteArray finalData = doc.toJson();
    qDebug() << finalData;

    reply = manager->put(request, finalData);
    connect(reply, &QNetworkReply::finished, [=](){
        if(reply->error() == QNetworkReply::NoError){
                QString contents = QString::fromUtf8(reply->readAll());
                qDebug() << contents;
            }
            else{
                QString err = reply->errorString();
                qDebug() << err;
            }
            reply->deleteLater();
    });

But I received the QByteArray like this: "{\n \"d\": {\n \"Duration\": 8,\n \"IgelId\": \"123456780XYZ\",\n \"LoadCumulLimit\": 25,\n \"Notes\": \"send from IGEL\"\n }\n}\n"

0

There are 0 best solutions below