如何将参数传递给通过 boost::process:child 执行的curl 请求?
我可以通过传递整个命令行,通过 boost::process::child
使用curl 执行http POST 请求。但是,我想通过 boost::process::args 传递参数,但我无法让它工作。
这有效:
const std::string cmdDiscord = "curl -X POST https://discord.com:443/api/webhooks/1234567890 -H \"content-type: application/json\" -d \"{\"content\": \"test\"}\"";
boost::process::child c(cmdDiscord); // this works
boost::process::child c(boost::process::cmd = cmdDiscord); // strangely, this doesn't work
我想使用 boost::process::args
但这失败了:
std::vector<std::string> argsDiscord {"-X POST",
"https://discord.com:443/api/webhooks/1234567890",
"-H \"content-type: application/json\"",
"-d \"{\"content\": \"test\"}\""};
boost::process::child c(boost::process::search_path("curl"), boost::process::args (argsDiscord));
错误是 curl: (55) Failed moving HTTP POST request
这是相当模糊的错误信息。我找不到任何调用curl的例子。有人对让它发挥作用有什么建议吗?
I'm able to execute a http POST request using curl via boost::process::child
by passing the entire command line. However, I would like to pass the arguments via boost::process::args
but I cannot get it work.
This works:
const std::string cmdDiscord = "curl -X POST https://discord.com:443/api/webhooks/1234567890 -H \"content-type: application/json\" -d \"{\"content\": \"test\"}\"";
boost::process::child c(cmdDiscord); // this works
boost::process::child c(boost::process::cmd = cmdDiscord); // strangely, this doesn't work
I want to use boost::process::args
but this fails:
std::vector<std::string> argsDiscord {"-X POST",
"https://discord.com:443/api/webhooks/1234567890",
"-H \"content-type: application/json\"",
"-d \"{\"content\": \"test\"}\""};
boost::process::child c(boost::process::search_path("curl"), boost::process::args (argsDiscord));
The error is curl: (55) Failed sending HTTP POST request
which is quite a vague error message. I couldn't find any examples calling curl. Does anyone have any suggestions on getting this to work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
应该是
因为命令解释器传递像
-X POST
这样的参数是两个参数,而不是一个。双引号也是 shell 语法。 shell 在命令行扩展期间解释(删除)它们。
或者,curl 接受短选项中的相邻值(没有空格)
It should be
Since command interpretators pass arguments like
-X POST
are two arguments, not one.The double quotes are a shell syntax as well. The shell interprets (removes) them during command line expansion.
Alternatively curl accepts adjacent values in short options (without space)