如何将参数传递给通过 boost::process:child 执行的curl 请求?

发布于 2025-01-09 08:58:38 字数 1078 浏览 1 评论 0原文

我可以通过传递整个命令行,通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝海 2025-01-16 08:58:38

应该是

std::vector<std::string> argsDiscord {"-X", "POST",
                                      "https://discord.com:443/api/webhooks/1234567890",
                                      "-H", "content-type: application/json",
                                      "-d", "{\"content\": \"test\"}"};

因为命令解释器传递像 -X POST 这样的参数是两个参数,而不是一个。

双引号也是 shell 语法。 shell 在命令行扩展期间解释(删除)它们。

或者,curl 接受短选项中的相邻值(没有空格)

std::vector<std::string> argsDiscord {"-XPOST",
                                      "https://discord.com:443/api/webhooks/1234567890",
                                      "-H", "content-type: application/json",
                                      "-d", "{\"content\": \"test\"}"};

It should be

std::vector<std::string> argsDiscord {"-X", "POST",
                                      "https://discord.com:443/api/webhooks/1234567890",
                                      "-H", "content-type: application/json",
                                      "-d", "{\"content\": \"test\"}"};

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)

std::vector<std::string> argsDiscord {"-XPOST",
                                      "https://discord.com:443/api/webhooks/1234567890",
                                      "-H", "content-type: application/json",
                                      "-d", "{\"content\": \"test\"}"};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文