Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
cURL C++ Example
#include <iostream>
#include <string>
#include <curl/curl.h>
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
((std::string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb;
int main(void)
CURL *curl;
CURLcode res;
std::string readBuffer;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
res = curl_easy_perform(curl);
std::cout << readBuffer << std::endl;
return 0;
Copy link

quangthanh010290 commented Apr 12, 2018

how can i use PUT or POST option in code? thank you!

Copy link

aftabnaveed commented Jul 18, 2018

when i tried url.c_str() I got segmantation fault error. passing readBuffer by reference is also working in my app.

url has to be converted using url.c_str()

Copy link

shashiranjansjce commented Dec 13, 2018

when i am giving wrong URL then it block with line
res = curl_easy_perform(curl);

is there any idea ,please suggest me.

Thanks in advance

Copy link

shashiranjansjce commented Dec 13, 2018

again posting more

int sendMsg(string toSend){

  int status = -1;
  CURL *curl;
 CURLcode res;
 string response;
  string url = "";
  string data = "name="+toSend;

  curl = curl_easy_init();

      curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
      curl_easy_setopt(curl, CURLOPT_POST, 1);
      curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str());
      curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
     curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);

    res = curl_easy_perform(curl);  /------------------------  block here-------------------/

      if(res == 0)



it blocked at line res = curl_easy_perform(curl);

if i am giving correct url then working fine but blocked with wrong url. it should block, it should return error code.
please provide me suggestion.

Copy link

sumanthboppana commented Apr 8, 2019

res = curl_easy_perform(curl);
What is the use of "CURLcode res" variable? you have not used it further in above program? And what is the returning value to res variable?
Please help me..

Copy link

scribnermj commented May 6, 2019

This code did work right out of the box for me.
With so many projects there are always little details.
This is a great example.
The follow-up message that includes the compiler line cinches it. -lcurl was essential to the compilation.

Copy link

abhishekaryan013 commented Aug 12, 2019

How can i run this code in code block ?

Copy link

supern64 commented Oct 7, 2019

Thank you so much for writing this as an example!

Copy link

pavel-shvetsov commented Oct 10, 2019


For those folks who are looking for cURL + HTTPS - curl team has nice example on their project page:

Also there are tons of useful code snippets for many use cases:

Copy link

ghost commented Dec 19, 2020

Where would one find the curl.h file in curl's source code? Is it on GitHub by any chance?

Incredible code nevertheless!

Copy link

sardar01 commented Feb 5, 2022

Works perfectly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment