cURL trouble with getting post response
May 19, 2017 at 12:26pm UTC
I'm trying to create a basic login program that's connected to server but when I try to save the response from a post request to a variable I wont get a response at all but if I don't try to save it I do get a response, I'm not really sure what I'm doing wrong, any help would be appreciated, cheers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
int post(string str1, string str2, string str3) {
string query = "user=" + str1 + "&pass=" + str2;
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, "mywebsite" );
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, query.c_str());
// when i comment out these 2 lines I get a response otherwise I don't
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_to_string);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &str3);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return 0;
}
Write to string
1 2 3 4
size_t write_to_string(void *ptr, size_t size, size_t count, void *stream) {
((string*)stream)->append((char *)ptr, 0, size*count);
return size*count;
}
Last edited on May 19, 2017 at 12:27pm UTC
Topic archived. No new replies allowed.