Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert this curl command to libcurl

Tags:

c++

curl

libcurl

I succeed to login www.tistory.com by using curl command like this.

curl -c cookie.txt -d "loginId=xxx&password=xxx&form_id=authForm" https://www.tistory.com/auth/login

And... to use it on my c++ project, I converted it to libcurl like this.

CURL *curl;
CURLcode res;
char *location;
long response_code;

curl = curl_easy_init();

if (curl) {
    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
    curl_easy_setopt(curl, CURLOPT_URL, "https://www.tistory.com/auth/login");
    curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "cookie.txt");

    const char* postData = "loginId=xxx&password=xxx&form_id=authForm";

    curl_easy_setopt(curl, CURLOPT_POST, 1L);
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postData);
    curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen(postData));

    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);

    res = curl_easy_perform(curl);

    ......
}

......

This converted code doesn't work. And cookie.txt file wasn't saved, too. I don't know where is wrong or what I missed. Somebody let me know. Thanks in advance.

Here is result.

enter image description here

like image 383
pigstoe Avatar asked Dec 16 '25 11:12

pigstoe


1 Answers

The best way to convert a curl command line to C code is to use the --libcurl code.c command line option.

When doing that and comparing the output with your code snippet, it would appear the only difference between the two programs is the lack of a User-Agent: header in your libcurl-using program.

like image 173
Daniel Stenberg Avatar answered Dec 19 '25 02:12

Daniel Stenberg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!