POST data from curl++ does not send
I have been trying to post data from within curlpp to a web script, but it does not seem to work. On the PHP end, I simply run var_dump($_POST); to print everything received back. The $_POST global appears empty on execution.
My C++ code is as follows:
std::stringstream out;
std::stringstream ss;
ss << "127.0.0.1/online.php";
try
{
curlpp::Cleanup clean;
curlpp::Easy request;
curlpp::options::WriteStream ws(&out);
request.setOpt(ws);
request.setOpt<curlpp::options::Url>(ss.str());
request.setOpt(new curlpp::options::Verbose(true));
std::list<std::string> header;
header.push_back("Con开发者_如何学运维tent-Type: application/octet-stream");
request.setOpt(new curlpp::options::HttpHeader(header));
std::string test = "abcd=abcd";
request.setOpt(new curlpp::options::PostFields(test));
request.setOpt(new curlpp::options::PostFieldSize(test.length()));
request.perform();
}
catch(curlpp::RuntimeError& e)
{
ss.str("");
ss << "Error making request of type " << op << ": " << e.what();
PrintError(ss.str());
return "";
}
std::cout << out.str() << std::endl;
Am I doing anything very obviously wrong here?
For client-to-server uploads, the standard specifies that the content type will be one of application/x-www-form-urlencoded
or multipart/form-data
. Because your web sever and/or PHP follow the standard, you should change your content type, in this case, to application/x-www-form-urlencoded
.
Your content-type
is wrong, use application/x-www-form-urlencoded
. See here.
精彩评论