简体   繁体   English

使用 cpp-httplib 的 POST 请求

[英]POST request with cpp-httplib

I have found this header only library called cpp-httplib , which seems to work fine for my purposes.我发现这个只有头文件的库叫做cpp-httplib ,它似乎适合我的目的。 I need to control a camera through HTTP requests.我需要通过 HTTP 请求控制相机。 For example, I can read the current position of the camera using:例如,我可以使用以下方法读取相机的当前位置:

httplib::Client cli("192.170.0.201", 8080);
auto res = cli.Get("/ptz.stats/present_pos");

which corresponds to the following Curl command;对应于以下 Curl 命令;

curl -X GET " http://192.170.0.201:8080/ptz.stats/present_pos " curl -X GET " http://192.170.0.201:8080/ptz.stats/present_pos "

Now, I want to move the camera using a POST request.现在,我想使用 POST 请求移动相机。 With curl, I can use following command to move the camera to left:使用 curl,我可以使用以下命令将相机向左移动:

curl -X POST " http://192.170.0.201:8080/ptz.cmd/?pan_left=50 " curl -X POST " http://192.170.0.201:8080/ptz.cmd/?pan_left=50 "

I want to make the exact same POST request from httplib using我想使用httplib发出完全相同的 POST 请求

httplib::Client cli("192.170.0.201", 8080);
httplib::Params params{
  { "pan_left", "50" }
};
auto p = cli.Post("/ptz.cmd", params);

and this does nothing.这没有任何作用。 I can see the camera and that curl command moves it.我可以看到相机和 curl 命令移动它。 So, am I translating the POST request to httplib format in a wrong way?那么,我是否以错误的方式将 POST 请求转换为httplib格式? How would you call that curl request in httplib ?你会如何在httplib调用 curl 请求?

PS: httplib might not be a popular library but it has neat documentation and I think anyone working with web requests and C++ can help. PS: httplib可能不是一个流行的库,但它有简洁的文档,我认为任何处理 Web 请求和 C++ 的人都可以提供帮助。

Related question: How are parameters sent in an HTTP POST request?相关问题: 如何在 HTTP POST 请求中发送参数?

POST parameters are not sent in the URI. POST 参数不在 URI 中发送。 From the cpp-httplib github , you can sent url-encoded parameters using:cpp-httplib github ,您可以使用以下方式发送 url 编码的参数:

cli.Post("/ptz.cmd", "pan_left=50", "application/x-www-form-urlencoded");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM