I am trying to access an URL, using CPPREST http_client :
http://www.20min.ch/rss/rss.tmpl?type=channel&get=68
I am receiving response code 302 for URL- redirection.
But when i try to access the same URL using CURL, I am receiving CURLE_OK.
Below are the 2 piece of code :
using CURL :
CURL *curl;
CURLcode res;
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if(curl){
curl_easy_setopt(curl, CURLOPT_URL, "http://www.20min.ch/rss/rss.tmpl?type=channel&get=68");
res = curl_easy_perform(curl);
if(res != CURLE_OK) {
cout<<"failed";
}
else {
cout<<"success";
}
curl_easy_cleanup(curl);
}
curl_global_cleanup();
The output is : success
using CPPREST :
std::string url_= "http://www.20min.ch/rss/rss.tmpl?type=channel&get=68";
try
{
http_client client1(U(url_));
uri_builder builder1(U(""));
client1.request(methods::GET, builder1.to_string()).then([=](http_response response)
{
cout<<"Response code is : "<<response.status_code();
});
}
catch(std::exception& e)
{
cout<<"response :"<<e.what();
}
The output is :: Response code is : 302
I do not understand why the two libs are behaving differently for same URL??
UPDATE :
I have also tried with :
http_client client1(utility::conversions::to_string_t(url_));
and
http_client client1(U("http://www.20min.ch/rss/rss.tmpl?type=channel&get=68"));
and
http_client client1(U("http://www.20min.ch/"));
but the response is same 302 with cpp rest. [ for cross checking bing example
is working fine]
UPDATE 2:
The method as explained by @Matt Weber seems very helpful and legit but i am continuously getting error code : 400 for that, So I tried the below things: I tried to set the host and port for the URL in uri_builder.
http_client client(U("http://www.20min.ch/rss/"));
uri_builder builder(U("/rss.tmpl"));
builder.append_query(U("type"), U("channel"));
builder.append_query(U("get"), U("68"));
builder.set_host(U("www.20min.ch"));
builder.set_port(U("80"));
client.request(methods::GET, builder.to_string()).then([=](http_response response)
{
cout<<"Received response status code: "<<response.status_code();
});
but still same 302.