/
rest.cpp
50 lines (40 loc) · 1.36 KB
/
rest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "rest.h"
Rest::Rest() {
srvAddress = "http://localhost:9000";
}
QtJson::JsonObject Rest::get(QString action){
url.setUrl(srvAddress + "/" + action);
request.setUrl(url);
QNetworkReply* currentReply = networkManager.get(request); // GET
QEventLoop loop;
QObject::connect(currentReply, SIGNAL(finished()), &loop, SLOT(quit()));
loop.exec();
QString data = (QString)currentReply->readAll();
bool ok;
QtJson::JsonObject result = QtJson::parse(data,ok).toMap();
if(!ok) {
return result;
} else {
return result;
}
}
QtJson::JsonObject Rest::post(QString action, QtJson::JsonObject content) {
url.setUrl(srvAddress + "/" + action);
request.setUrl(url);
QByteArray jsonString = QtJson::serialize(content);
QByteArray postDataSize = QByteArray::number(jsonString.size());
request.setRawHeader("Content-Type", "application/json");
request.setRawHeader("Content-Length", postDataSize);
QNetworkReply* currentReply = networkManager.post(request,jsonString);
QEventLoop loop;
QObject::connect(currentReply, SIGNAL(finished()), &loop, SLOT(quit()));
loop.exec();
QString data = (QString)currentReply->readAll();
bool ok;
QtJson::JsonObject result = QtJson::parse(data,ok).toMap();
if(!ok) {
return result;
} else {
return result;
}
}