最近公司要我項(xiàng)目里弄http 請求,我沒用過。探索了這么點(diǎn)東西。
第一步:用vscode? 和它的 rest client插件完成post。
在文件里填入大數(shù)據(jù)同事給我的內(nèi)容,右鍵send request,就拉取到了這個車的點(diǎn)位們。
?第二步;開始寫代碼,文件目錄如下,include里放了json的頭文件。
?
?
代碼如下:測試http.cpp
#include <iostream>
#include "httplib.h"
#include <nlohmann/json.hpp>
#include <Windows.h>
#include <string>
using namespace std;
using json = nlohmann::json;
int main()
{
using json = nlohmann::json;
json j_patch = R"({
"did": "BCT5901642495177",
"gpsStartTime" : "2022-11-14 15:11:35",
"gpsEndTime" : "2022-11-14 15:11:29",
"desc" : false,
"pageSize" : 86400,
"startRowKey" : "",
"showColumns" : [
"2602",
"2603",
"3014"
] ,
"filterList" : [
{
"column": "2601",
"op" : "eq",
"value" : "0",
"filterIfMissing" : true
}
] ,
"parseValue": false
})"_json;
#if 1
// 創(chuàng)建HTTP client 對象
httplib::Client cli("192.168.13.73", 30001);
// 注意這里設(shè)置時間的話有2種方式,(time_t sec, time_t usec)
//cli.set_connection_timeout(300);
//cli.set_read_timeout(300);
// 創(chuàng)建header
httplib::Headers headers = {
{ "content-type", "application/json" }
};
//auto res = cli.Post("/post", headers, params);
// 參數(shù)準(zhǔn)備
string strvalue = j_patch.dump();
cout << strvalue << endl;
// 發(fā)送請求
auto res = cli.Post("/hbase/canList", headers, strvalue, "application/json");
cout << res->status << endl;
cout << res->body << endl;
#if 0
// 把結(jié)果弄成json, 然后從json中取出data項(xiàng),
auto jj = json::parse(res->body);
json hk = jj["data"];
string ss = jj.dump();
cout << ss << endl;
#endif
}
前面參數(shù)json 部分還可以用另一種方式:文章來源:http://www.zghlxwxcb.cn/news/detail-509672.html
json bodyParamJson;
bodyParamJson["did"] = "YXZ2612204084017";
bodyParamJson["gpsStartTime"] = "2000-01-13 00:00:00";
bodyParamJson["gpsEndTime"] = "2099-01-13 23:59:59";
bodyParamJson["desc"] = false;
bodyParamJson["pageSize"] = 86400;
bodyParamJson["startRowKey"] = "";
bodyParamJson["parseValue"] = false;
bodyParamJson["showColumns"] = R"([
"2602",
"2603",
"3014"
])"_json;
bodyParamJson["filterList"] = R"([
{
"column": "2601",
"op": "eq",
"value": "0",
"filterIfMissing": true
}
])"_json;
string ss = bodyParamJson.dump();
這樣就能配置param 參數(shù)了。文章來源地址http://www.zghlxwxcb.cn/news/detail-509672.html
到了這里,關(guān)于C++ 使用nlohmann/json和cpp-httplib 完成post請求的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!