时间:2019-05-16 标签:c++libcurljsonrest
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5707957/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
c++ libcurl json rest
提问by Philip
I am trying to download a json file from a REST webpage in C++ with libcurl. The following code works if I go to the webpage but it doesnt download if I try to access the json ....
我正在尝试使用 libcurl 从 C++ 中的 REST 网页下载一个 json 文件。如果我转到网页,以下代码有效,但如果我尝试访问 json 则不会下载....
I think it should be an easy fix but I cant find any reference to this ...
我认为这应该是一个简单的解决方法,但我找不到任何对此的参考......
If I go to webpage it opens the json but this code only returns text/html; charset=utf-8
如果我转到网页,它会打开 json,但此代码仅返回 text/html;字符集=utf-8
??????????
?????????
CURL *curl;
CURLcode res;
struct curl_slist *headers=NULL; // init to NULL is important
headers = curl_slist_append(headers, "Accept: application/json");
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "http://web.com/api/json/123");
curl_easy_setopt(curl, CURLOPT_HTTPGET,1);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
//curl_easy_setopt(curl, CURLOPT_URL, "http://web.com/123.html");//this works!!!
res = curl_easy_perform(curl);
if(CURLE_OK == res) {
char *ct;
/* ask for the content-type */
res = curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &ct);
if((CURLE_OK == res) && ct)
printf("We received Content-Type: %s\n", ct);
}
}
/* always cleanup */
curl_easy_cleanup(curl);
回答by Philip
std::string ServerContent::DownloadJSON(std::string URL)
{
CURL *curl;
CURLcode res;
struct curl_slist *headers=NULL; // init to NULL is important
std::ostringstream oss;
headers = curl_slist_append(headers, "Accept: application/json");
headers = curl_slist_append(headers, "Content-Type: application/json");
headers = curl_slist_append(headers, "charset: utf-8");
curl = curl_easy_init();
if (curl)
{
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_URL, URL.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPGET,1);
curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,writer);
res = curl_easy_perform(curl);
if (CURLE_OK == res)
{
char *ct;
res = curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &ct);
if((CURLE_OK == res) && ct)
return *DownloadedResponse;
}
}
curl_slist_free_all(headers);
}
static std::string *DownloadedResponse;
static int writer(char *data, size_t size, size_t nmemb, std::string *buffer_in)
{
// Is there anything in the buffer?
if (buffer_in != NULL)
{
// Append the data to the buffer
buffer_in->append(data, size * nmemb);
// How much did we write?
DownloadedResponse = buffer_in;
return size * nmemb;
}
return 0;
}
回答by Diego Sevilla
I think this has to do with the configuration on the HTTP server. First, you should be sending some type of indication of what type you're expecting, for example, adding an Accept
header like this:
我认为这与 HTTP 服务器上的配置有关。首先,您应该发送某种类型的指示,表明您期望的是什么类型,例如,添加这样的Accept
标头:
Accept: application/json
If you don't specify what you're expecting, the server may return the default HTML in the response header Content-Type
, and that's what curl
says to you.
如果你没有指定你所期望的,服务器可能会在响应头中返回默认的 HTML Content-Type
,这就是你curl
所说的。
回答by holtavolt
Have you tried this approach from the tutorial code at the curl site? http://curl.haxx.se/libcurl/c/sepheaders.html
您是否从 curl 站点的教程代码中尝试过这种方法? http://curl.haxx.se/libcurl/c/sepheaders.html