1

I am building a small application which needs to do get requests to an API.

I use libcurl which works great if I provide the complete URL directly within the function, but if I do it inside of a function with the URL as a parameter it fails instantly with the error CURLE_COULDNT_RESOLVE_HOST(6).

So I know it's not a DNS problem since I can resolve it if I provide the URL directly.

Here is my current function.

std::string winget(std::string url)
{

    CURL *curl;
    CURLcode res;
    std::string readBuffer;

    curl = curl_easy_init();
    if (curl) {
        curl_easy_setopt(curl, CURLOPT_URL, url);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
        res = curl_easy_perform(curl);
        curl_easy_cleanup(curl);
    }
        return readBuffer;
}

//and I call it like this :

winget("example.org");

Basically if I directly replace the url param in the function with "example.org" it works. So I don't really know what to do with it.

Thanks a lot in advance. :)

1 Answer 1

5

Found the solution here: URL Variable passing into Curl

Basically you need to provide the URL as a null-terminated string. So if the parameter is a string, use .c_str() or get a char* from that string.

Sign up to request clarification or add additional context in comments.

1 Comment

Same problem might also occur with other parameters for curl. Remember that libcurl ist a C library, not a C++ library. That means that libcurl cannot use std::string for strings. Instead, char* is used for any string-like content.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.