'libcurl C++: get cookies with multi interface
I want to get the cookies from a POST request.
curl_global_init(CURL_GLOBAL_DEFAULT);
http_handle = curl_easy_init();
curl_easy_setopt(http_handle, CURLOPT_URL, url);
curl_easy_setopt(http_handle, CURLOPT_COOKIEFILE, "");
curl_easy_perform(http_handle);
curl_easy_getinfo(http_handle, CURLINFO_COOKIELIST, &cookies);
In the case above, cookies are retrieved OK.
I'm trying a similar approach with multi interface. I change the latest two lines above for the following code:
int still_running = 1;
multi_handle = curl_multi_init();
curl_multi_add_handle(multi_handle, http_handle);
do {
curl_multi_perform(multi_handle, &still_running);
} while (still_running)
struct CURLMsg *msg;
int queued;
do {
msg = curl_multi_info_read(multi_handle, &queued);
if ((msg) && (msg->msg == CURLMSG_DONE)) {
CURL *e = msg->easy_handle;
CURLcode res = curl_easy_getinfo(e, CURLINFO_COOKIELIST, &cookies);
// res is OK, but cookies are empty
long status;
CURLcode res2 = curl_easy_getinfo(e, CURLINFO_RESPONSE_CODE, &status);
// res2 is OK, and status gets the status code
}
} while(msg)
In the code above, I can get for example the response code, but the cookies are empty.
Maybe the multi_info_read does not work to get the cookies? Or could I be missing something?
Solution 1:[1]
Please post an minimal reproducible example. The code in the question has a lot of errors and could not be compiled.
My code works well.
#include <iostream>
#include <curl/curl.h>
int main () {
CURL* http_handle = curl_easy_init();
curl_easy_setopt(http_handle, CURLOPT_URL, "http://httpbin.org/cookies/set?key=value");
curl_easy_setopt(http_handle, CURLOPT_COOKIEFILE, "");
int still_running = 1;
CURLM* multi_handle = curl_multi_init();
curl_multi_add_handle(multi_handle, http_handle);
do {
curl_multi_perform(multi_handle, &still_running);
} while (still_running);
struct CURLMsg* msg;
int queued;
do {
msg = curl_multi_info_read(multi_handle, &queued);
if ((msg) && (msg->msg == CURLMSG_DONE)) {
CURL* e = msg->easy_handle;
curl_slist* cookies;
CURLcode res = curl_easy_getinfo(e, CURLINFO_COOKIELIST, &cookies);
std::cout << "\nSet-Cookie: " << cookies->data << std::endl;
}
} while (msg);
curl_multi_remove_handle(multi_handle, http_handle);
curl_multi_cleanup(multi_handle);
curl_easy_cleanup(http_handle);
return 0;
}
Output:
% ./a.out
...
Set-Cookie: httpbin.org FALSE / FALSE 0 key value
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | 273K |
