When I do
curl --get --cookie-jar mycookie.cookie http://mypage/page/
it will store a cookie as mycookie.cookie
and then when I do
curl --cookie mycookie.cookie --data "field1=field1" --data "field2=field2" --data csrfmiddlewaretoken=(csrf token) http://mypage/page/register/
the csrf token I get through a cat mycookie.cookie and fill it manually in.
This works. It does what I want.
So now I want to use libcurl with C to do this. Following the doc I have this:
CURL *curl;
CURLcode res;
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, http://mypage/page/);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "");
res = curl_easy_perform(curl);
res = curl_easy_getinfo(curl, CURLINFO_COOKIELIST, &cookies);
curl_easy_setopt(curl, CURLOPT_URL, http://mypage/page/register/);
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookies);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "field1=field1;field1=field1;csrfmiddlewaretoken=(csrf token)");
res = curl_easy_perform(curl);
printf("Erasing curl's knowledge of cookies!\n");
curl_easy_setopt(curl, CURLOPT_COOKIELIST, "ALL");
curl_slist_free_all(cookies);
}
curl_global_cleanup();
return 0;
So this will pass the cookie but throw an error for missing fields. So I thought this line will post all the fields:
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "field1=field1;field1=field1;csrfmiddlewaretoken=(csrf token)");
I also tried passing all the fields through this line:
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookies);
by riding everything in a char array
I also tried replacing , with ; but nothing works.
I don't think I wrote anything wrong, it looks more like the post overwrite each other, because if I run the programm without the COOKIEFILE line it says missing cookie.
Any idea, how I post all necessary pieces information?
EDIT
Ok, I got it to work through these two posts here and here and Daniel Stenberg
So I have the same code just without
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookies);