Skip to content

Instantly share code, notes, and snippets.

Embed
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, "http://www.google.com");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
std::cout << readBuffer << std::endl;
}
return 0;
}
@shashiranjansjce
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 = "http://129.2.2.2/test.php";
  string data = "name="+toSend;
  curl_global_init(CURL_GLOBAL_ALL);

  curl = curl_easy_init();

  if(curl){
      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.

@sumanthboppana
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..

@scribnermj
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.

@abhishekaryan013
Copy link

abhishekaryan013 commented Aug 12, 2019

How can i run this code in code block ?

@supern64
Copy link

supern64 commented Oct 7, 2019

Thank you so much for writing this as an example!

@pavel-shvetsov
Copy link

pavel-shvetsov commented Oct 10, 2019

Hello,

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: https://curl.haxx.se/libcurl/c/example.html

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!

@sardar01
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