Skip to content
The Chat Archive
Human-written demonstration (OpenAssistant volunteers)· OpenAssistant (human-written) OpenAssistant Conversations (oasst2)

How to make a HTTP request in C++ code, give me 2 examples using pure C++ and using…

In OpenAssistant data, “assistant” turns were written by human volunteers as demonstrations. They are not model outputs.
  1. User

    How to make a HTTP request in C++ code, give me 2 examples using pure C++ and using external library, which is more efficient?

  2. Assistant

    To make an HTTP request in C++, you can either write the code yourself using only the standard C++ libraries, or use an external library that provides a higher-level API.

    Here are two examples of making an HTTP request using pure C++:

    c

    #include #include #include #include #include #include #include <unordered_map> #include <curl/curl.h>

    std::string get_url(const std::string& url) { CURL* curl = curl_easy_init(); if (!curl) { return ""; }

    std::stringstream ss;
    curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, [&](char* data, std::size_t size, std::size_t nmemb, void* userp) -> std::size_t {
        ss.write(data, size * nmemb);
        return size * nmemb;
    });
    curl_easy_perform(curl);
    curl_easy_cleanup(curl);
    
    return ss.str();
    

    }

    int main() { std::string response = get_url("https://www.example.com"); std::cout << response << std::endl;

    return 0;
    

    } And here's an example using an external library, such as libcurl:

    c

    #include #include #include #include #include #include #include <unordered_map> #include <curl/curl.h>

    std::string get_url(const std::string& url) { CURL* curl = curl_easy_init(); if (!curl) { return ""; }

    std::stringstream ss;
    curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, [&](char* data, std::size_t size, std::size_t nmemb, void* userp) -> std::size_t {
        ss.write(data, size * nmemb);
        return size * nmemb;
    });
    curl_easy_perform(curl);
    curl_easy_cleanup(curl);
    
    return ss.str();
    

    }

    int main() { std::string response = get_url("https://www.example.com"); std::cout << response << std::endl;

    return 0;
    

    } As for efficiency, using an external library such as libcurl can be more efficient because it provides a higher-level API that abstracts away much of the low-level details involved in making an HTTP request. This makes it easier to write code that is more concise and less error-prone, and also eliminates the

  3. User

    Please complete the last paragraph.

  4. Assistant

    Sorry! Here's the last paragraph completed:

    As for efficiency, using an external library such as libcurl can be more efficient because it provides a higher-level API that abstracts away much of the low-level details involved in making an HTTP request. This makes it easier to write code that is more concise and less error-prone, and also eliminates the need to re-implement common features, such as error handling, that are already included in the library. Additionally, libcurl is a well-maintained and widely-used library, so it is likely to have better performance than hand-rolled code. That being said, in some cases, using a library might not be necessary and writing your own code from scratch may provide more control and customization over the HTTP request process, especially if performance is a primary concern. The decision of which approach to use depends on the specific requirements and trade-offs of each project.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.