curl 请求的用户输入

User input for a curl request

看来我不能将变量用作 curl 的 post 参数。我不明白为什么它不起作用。我试着打印这个变量,这个变量和我想要的完全一样……如果我直接把它作为参数:"useremail=testuser&password=lol123&action=login&remember=remember";它工作正常,但我想使用用户输入变量...

int main(void){
CURL *curl;
CURLcode res;
std::string readBuffer;

std::string testinput;

std::cin >> testinput;

std::string data = "useremail=" + testinput + "&password=lol123&action=login&remember=remember";

curl = curl_easy_init();
if (curl) {



    curl_easy_setopt(curl, CURLOPT_URL, "https://manager.domain.pro/api.php");
    /* Now specify the POST data */
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);

    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
    res = curl_easy_perform(curl);

    std::cout << readBuffer << std::endl;

    curl_easy_cleanup(curl);

}
return 0;}
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, ...);

没有将 std::string 作为第三个参数,而是指向 char 的指针,因此您应该传递它 data.c_str() 并希望 curl_easy_setup() 不会尝试修改数据,因为 std::string::c_str() returns a char const *.