1

I get redirected to a page with address like http://example.com#foo=bar. I want to get foo=bar part of it. The whole thing would be ok too.

I found this thing:

char * url;
curl_easy_getinfo(myHandle, CURLINFO_EFFECTIVE_URL, &url);

I don't know english well to find information myself. Every time I want to find it, I find information on getting the page into string variable.

Code:

std::string readBuffer;
curl_global_init( CURL_GLOBAL_ALL);
CURL * myHandle;
CURLcode result;
myHandle = curl_easy_init();
curl_easy_setopt(myHandle, CURLOPT_COOKIEJAR, "coo.txt");
curl_easy_setopt(myHandle, CURLOPT_COOKIEFILE, "coo.txt");
curl_easy_setopt(myHandle, CURLOPT_URL, "https://www.google.ru/#q=stack");
curl_easy_setopt(myHandle, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(myHandle, CURLOPT_WRITEDATA, &readBuffer);
curl_easy_setopt(myHandle, CURLOPT_FOLLOWLOCATION, 1L);
result = curl_easy_perform(myHandle);
char * ch_cur_url;
result = curl_easy_getinfo(myHandle, CURLINFO_EFFECTIVE_URL,
        &ch_cur_url);
printf("%s\n", ch_cur_url);

Outputs https://www.google.ru/

When I wanted https://www.google.ru/#q=stack

ArtemGr
  • 11,684
  • 3
  • 52
  • 85
Ilgiz Mustafin
  • 414
  • 1
  • 4
  • 15
  • You may find this answer useful: [How to split a string in C++?](http://stackoverflow.com/a/236803/1413395) – πάντα ῥεῖ Jul 13 '14 at 16:20
  • @πάνταῥεῖ well, maybe. I know how to split strings. I just can't get the whole string. Is not that clear enough? How to make it clearer? – Ilgiz Mustafin Jul 13 '14 at 16:25
  • So what's the actual result for `char * url;`?? Yes, you need to make your question clearer a lot. – πάντα ῥεῖ Jul 13 '14 at 16:27
  • @πάνταῥεῖ what else? Maybe I had to ask that before. – Ilgiz Mustafin Jul 13 '14 at 16:29
  • _'what else?'_ Provide appropriate snippets of code you have tried, what you have expected to be their outcome, and what you actually got. In other words: Provide a [MCVE](http://stackoverflow.com/help/mcve). – πάντα ῥεῖ Jul 13 '14 at 16:32
  • Better, yes. `curl_easy_setopt(myHandle, CURLOPT_WRITEDATA, &readBuffer);` looks suspicious, does this even compile? From the [docs](http://curl.haxx.se/libcurl/c/curl_easy_setopt.html): _"Strings passed to libcurl as `char *` arguments"_ As for the `curl_easy_getinfo()` I can't actually spot how to get the fully requested URL. – πάντα ῥεῖ Jul 13 '14 at 17:18
  • @πάνταῥεῖ `&readBuffer` compiles all right. Can I get the fully requested URL from Haders? – Ilgiz Mustafin Jul 13 '14 at 17:27
  • _'compiles all right'_ Suspect though! A `std::string*` isn't the same thing as a `char *`! _'Can I get the fully requested URL from Haders?'_ Probably, yes. – πάντα ῥεῖ Jul 13 '14 at 17:29

1 Answers1

1

cURL removes the "fragment identifier" from the URL before making a request, as per the bug reports (1, 2). See also this patch. Thus the "fragment identifier" is not available as part of the CURLINFO_EFFECTIVE_URL.

If the "fragment identifier" is returned as part of a redirect (e.g. the Location HTTP header) and you can't get it any other way, then you may use the debug modes to peek on the communications between the cURL and the servers and extract the "fragment identifier" yourself. To that end you'll need to setup either CURLOPT_DEBUGFUNCTION or CURLOPT_HEADERFUNCTION.

P.S. A bit of advise: Googling the relevant information was very easy. First thing I did was to learn the "official" name of the #foo=bar. To get it I visited Wikipedia at URL and was brought to Fragment identifier. After that, Googling with the "curl fragment" netted the relevant parts. If you're looking for something, learn it's proper name.

ArtemGr
  • 11,684
  • 3
  • 52
  • 85
  • After a nit of googling, it seems to me that my question is not clear. It seems that fragment from `https://www.google.ru/#q=stack` will be trimmed. Is that so? – Ilgiz Mustafin Jul 17 '14 at 18:55
  • The "fragment identifier" is trimmed, yes. Check the wikipedia article, the browser should never send the fragment identifier to the server and so it's not a part of the effective URL. – ArtemGr Jul 17 '14 at 19:07