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

How can I pass args to a curl request executed via boost::process:child?

我可以通过 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 sending HTTP POST request,这是一条非常模糊的错误消息。我找不到任何调用 curl 的示例。有人对让它起作用有什么建议吗?

应该是

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 接受短选项中的相邻值(没有 space)

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