开发者

Why do I get a CURLE_URL_MALFORMAT when trying to http post?

Here's the code (extracted from an existing application):

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(cu开发者_运维问答rl);

bool success = (res == CURLE_OK);

curl_easy_cleanup(curl);

The value of res is CURLE_URL_MALFORMAT. Is this URL not compatible with curl?


Ah, simple mistake, I need to pass char * to curl_easy_setopt and not string. To fix this I've just used .c_str() like so:

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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜