为什么我在尝试 http post 时收到 CURLE_URL_MALFORMAT?

Nic*_*ton 4 post curl http

这是代码(从现有应用程序中提取):

CURL *curl = curl_easy_init();
_ASSERTE(curl);

string url = "http://127.0.0.1:8000/";

char *data = "mode=test";
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
curl_easy_setopt(curl, CURLOPT_URL, url);
CURLcode res = curl_easy_perform(curl);

bool success = (res == CURLE_OK);

curl_easy_cleanup(curl);
Run Code Online (Sandbox Code Playgroud)

res的值为CURLE_URL_MALFORMAT。这个URL与curl不兼容吗?

Nic*_*ton 10

啊,简单的错误,我需要传递char *curl_easy_setopt而不是string。为了解决这个问题,我只是.c_str()这样使用:

CURL *curl = curl_easy_init();
_ASSERTE(curl);

string url = "http://127.0.0.1:8000/";

char *data = "mode=test";
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
CURLcode res = curl_easy_perform(curl);

bool success = (res == CURLE_OK);

curl_easy_cleanup(curl);
Run Code Online (Sandbox Code Playgroud)