c++ http server端接收post请求参数
·
#include "third/httplib/httplib.h"
#include "third/nlohmann/json.hpp"
#include <vector>
using namespace std;
using namespace httplib;
using namespace nlohmann;
int t=0;
int main()
{
Server server;
server.Post("/image_vector", [](const httplib::Request &req, httplib::Response &res)
{
t = t + 1;
cout << "第 " << t << " 次调用服务" << endl;
string imgUrl, imgType;
string width, height;
if (req.has_param("imgUrl"))
{
imgUrl = req.get_param_value("imgUrl");
cout << "imgUrl: " << imgUrl << endl;
}
if (req.has_param("imgType"))
{
imgType = req.get_param_value("imgType");
cout << "imgType: " << imgType << endl;
}
if (req.has_param("width"))
{
width = req.get_param_value("width");
cout << "width: " << width << endl;
}
if (req.has_param("height"))
{
height = req.get_param_value("height");
cout << "height: " << height << endl;
}
vector<float> feature = {-0.1,0.98,1.5,0.2,0.003};
json j = {
{"imgFeature", feature},
{"imgUrl", imgUrl},
{"imgType", imgType}
};
res.set_content(j.dump(), "text/plain");
});
server.listen("0.0.0.0", 10000);
}
python测试
不传参数/传参数
import requests
params = {'imgUrl': 'http://xx.x.jpg',
'imgType': 'jpg',
'width': 300,
'height': 600}
res = requests.post('http://ubuntu机器ip:10000/image_vector')
print(res.json())
print('---------------')
res = requests.post('http://ubuntu机器ip:10000/image_vector', params=params)
print(res.json())
{'imgFeature': [-0.10000000149011612, 0.9800000190734863, 1.5, 0.20000000298023224, 0.003000000026077032], 'imgType': '', 'imgUrl': ''}
---------------
{'imgFeature': [-0.10000000149011612, 0.9800000190734863, 1.5, 0.20000000298023224, 0.003000000026077032], 'imgType': 'jpg', 'imgUrl': 'http://xx.x.jpg'}
服务终端打印

curl测试
传参数

服务终端打印

更多推荐
所有评论(0)