简体   繁体   中英

how to use cookies and postfields with libcurl in C?

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);
  1. Your string sent to CURLOPT_POSTFIELDS is different than the string your command line uses! When you use --data multiple times, curl will concatenate the strings with an ampersand (&) in between, while your C code uses a semicolon.

  2. You pass the wrong input to CURLOPT_COOKIEFILE. It accepts a file name, nothing else. But you don't need to use that option in your second request, since you enabled the "cookie engine" in the first request, all cookies recieved in the first request are kept in the curl handle and will be used in the subsequent request anyway when reusing the handle.

  3. To extract the csrf_token cookie, you would use the CURLINFO_COOKIELIST and parse through the list of cookies to find it, extract the contents and use that in the subsequent POST request.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM