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 的示例。有沒有人有任何建議讓這個作業?
uj5u.com熱心網友回復:
它應該是
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\"}"};
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/436723.html
