命令行选项正在工作,下面是 wokring 请求
curl -s -H "Content-Type: application/json" https://speech.googleapis.com/v1/speech:recognize?key=apikey -d @sync-request.json
尝试使用 libcurl 执行相同的操作,添加 json 数据文件失败...在 curl::PostFields 中直接将 json 数据作为字符串传递时工作。
在下面的示例中,将 apikey 替换为 mydev 密钥。对于大文件大小需要此选项。
#include <iostream>
#include <curlpp/Options.hpp>
#include <curlpp/Easy.hpp>
#include <curlpp/cURLpp.hpp>
#include <sstream>
#include <future>
#include <curlpp/Exception.hpp>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cerrno>
size_t WriteCallback(char* ptr, size_t size, size_t nmemb, void *f)
{
FILE *file = (FILE *)f;
cout<<"file write"<<endl;
return fwrite(ptr, size, nmemb, file);
}
std::future<std::string> invoke(std::string const& url) {
return std::async(std::launch::async,
[](std::string const& url) mutable {
std::list<std::string> header;
header.push_back("Content-Type: application/json");
FILE* file = fopen("sync-request.json", "wb");
curlpp::options::WriteFunctionCurlFunction myFunction(WriteCallback);
curlpp::OptionTrait<void *, CURLOPT_WRITEDATA> myData(file);
curlpp::Cleanup clean;
curlpp::Easy r;
r.setOpt(new curlpp::options::Url(url));
r.setOpt(new curlpp::options::HttpHeader(header));
r.setOpt(myFunction);
r.setOpt(myData);
std::ostringstream response;
r.setOpt(new curlpp::options::WriteStream(&response));
r.perform();
std::cout<<std::string(response.str());
return std::string(response.str());
}, url);
}
int main(int argc, char **argv) {
invoke("https://speech.googleapis.com/v1/speech:recognize?key=apikey");
return 0;
}
抛出错误
<p>The requested URL <code>/v1/speech:recognize?key=apikey</code> was not found on this server.