在Qt5的帮助下,通过POST请求将JSON文件发送到服务器时有点问题。我给你一张我的程序:POST在Qt5中发送JSON文件的请求
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QByteArray jsonDocument("{}");
QUrl url("/?json=");
QNetworkRequest request(url);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
QNetworkAccessManager manager;
QNetworkReply* reply = manager.post(request, jsonDocument);
while(!reply->isFinished()) {
a.processEvents();
}
std::cout << "Ok, Server response : " << response.toStdString() << std::endl;
return 0;
}
由于这种不上,我要送我的文件,我测试我的代码就像在测试服务器上的服务器上运行 - >当你发送一个json到服务器时,它会回答你的json是否有效。
但我实际的代码我收到此:
Ok, Server response : {
"error": "A JSONObject text must begin with '{' at 1 [character 2 line 1]",
"object_or_array": "object",
"error_info": "This error came from the org.json reference parser.",
"validate": false
}
所以,如果我发送相同的最小JSON对象{}用卷成那样:
curl -X POST -d "json={}"
答案在逻辑上是好的:
{
"object_or_array": "object",
"empty": true,
"parse_time_nanoseconds": 10874,
"validate": true,
"size": 0
}
我想我错过了一段理解我的Qt代码或POST请求的机制。
-11-26
AuraHxC