Unsolved How to create special json format to post
-
@ztencmcp said in How to create special json format to post:
the server responses json format error when i post data with QNetworkAccessManager.
How do you post? Please show the code from document.setObject(json); to
QNetworkAccessManager::post()
-
@ztencmcp
I don't know, I'm not a JSON expert, but like @Christian-Ehrlicher I thought the JSON you show would not be valid. Look at this bit:/"username/": /"admin/"
You have a
/
outside the quoted string for both the property name & value. Is that really correct?? -
you can see the pic, the right one i created it with cpprestsdk web::json::value serialize() fucntion, the wrong one i created it with qt5 QJsonObject。http server only accepts the right one。
-
-
@JKSH
here's the code. Thanks.And i used cpprestsdk to do the same thing and it's right.
-
When you look at the content of the QByteArray
You'll see, there is no " at the beginning or at the end. The Qt creates the same "JSON string" as cpprestsdk
the error is somewhere else.
-
@ztencmcp
Can we please be clear about one thing: in your first code it used/"
, in the stuff you are pasting now it uses\"
. Are you saying that in JSON one can use either/
or\
as the "escape" character?? -
@KroMignon Thanks for advice, pls see the pics below.
-
@ztencmcp You did NOT specify in your POST request the format!
QNetworkRequest request(QUrl("https://example.com")); request.setRawHeader("User-Agent", "My app name v0.1"); request.setRawHeader("X-Custom-User-Agent", "My app name v0.1"); request.setRawHeader("Content-Type", "application/json"); request.setRawHeader("Content-Length", postdata.size()); manager->post(request, postdata);
or
QNetworkRequest request(QUrl("https://example.com")); request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json"); manager->post(request, postdata);
Should do the job!
-
@KroMignon Thanks, let me try.