Files
squirrelVsis/客户端.cpp
2025-10-03 19:05:22 +08:00

42 lines
1.2 KiB
C++

#include "httplib.h"
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include <iostream>
#include <string>
std::string buildJsonData(const std::string& str1, const std::string& str2) {
rapidjson::StringBuffer sb;
rapidjson::Writer<rapidjson::StringBuffer> writer(sb);
writer.StartObject();
writer.Key(str1.c_str()); // 使用 str1 作为 key
writer.String(str2.c_str()); // 使用 str2 作为 value
writer.EndObject();
return sb.GetString();
}
int main() {
httplib::Client cli("http://127.0.0.1:26000");
// 准备数据
std::string str1 = "load.nut";
std::string str2 = "\r\nlocal job = sq_getJob();\r\nprint(job);\r\n ";
// 构建 JSON
std::string jsonStr = buildJsonData(str1, str2);
std::cout << "发送的 JSON: " << jsonStr << std::endl;
// 发送数据
auto res = cli.Post("/send_data", jsonStr, "application/json");
if (res && res->status == 200) {
std::cout << "服务端回复: " << res->body << std::endl;
} else {
std::cerr << "发送失败,状态码: " << (res ? res->status : 0) << std::endl;
}
return 0;
}