lib curl in c++ disable printing - c++

i got a small program from http://curl.haxx.se/ and while i run it always prints the webpage how can i disable the printing function
#include <iostream>
#include <curl/curl.h>
using namespace std;
int main() {
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "http://google.com");
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION,1);
res = curl_easy_perform(curl);
/* always cleanup */
curl_easy_cleanup(curl);
}
return 0;
}

You need to set up a CURLOPT_WRITEFUNCTION to make it not use stdout.
There is an explanation here (under CURLOPT_WRITEFUNCTION):
http://curl.haxx.se/libcurl/c/curl_easy_setopt.html
and here (Under "Handling the Easy libcurl):
http://curl.haxx.se/libcurl/c/libcurl-tutorial.html
Basically adding the function:
size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp)
{
return size * nmemb;
}
and calling
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
Should do it.

You can still get diagnostic messages. To stop these either change or add the following line:
curl_easy_setopt (curl, CURLOPT_VERBOSE, 0L); //0 disable messages

To write data into file instead of printing, give a file descriptor as:
FILE *wfd = fopen("foo.txt", "w");
...
curl_easy_setopt(curl, CURLOPT_WRITEDATA, wfd);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL);

What worked for me was using the CURLOPT_NOBODY option in the code, referenced here: http://curl.haxx.se/libcurl/c/curl_easy_setopt.html#CURLOPTNOBODY
#include <iostream>
#include <curl/curl.h>
using namespace std;
int main() {
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "http://google.com");
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION,1);
//USING CURLOPT NOBODY
curl_easy_setopt(curl, CURLOPT_NOBODY,1);
res = curl_easy_perform(curl);
/* always cleanup */
curl_easy_cleanup(curl);
}
return 0;
}

Related

libcurl simple C file downloader example immediately returns

I tried compiling both the examples on this question: Download file using libcurl in C/C++
Here's one of them:
#include <stdio.h>
#include <curl/curl.h>
int main(void)
{
CURL *curl;
FILE *fp;
CURLcode res;
char *url = "http://stackoverflow.com";
char outfilename[FILENAME_MAX] = "page.html";
curl = curl_easy_init();
if (curl)
{
fp = fopen(outfilename,"wb");
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
fclose(fp);
}
return 0;
}
the problem is that this example, when run, immediately returns and I get a blank file. Why? I modified to
if (curl)
{
fp = fopen(outfilename,"wb");
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
fclose(fp);
} else {
printf("error\n");
}
but I see no error. I tried compiling in both C++ and C, I get the same result on both.
I had the same issue and I fixed it by:
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, true);
According to https://curl.se/libcurl/c/CURLOPT_FOLLOWLOCATION.html, true tells the library to follow HTTP location.

C++: How can I Send binary data(protobuf data) using HTTP post request through a curl call

I am trying trying to make a POST request on a url with protobuf data. I don't know how/ where to add binary data. Below is my C++ program.
"
void sendContent()
{
using namespace std;
int Error = 0;
//CString str;
CURL* curl;
CURLcode res;
struct curl_slist *headerlist = NULL;
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
headerlist = curl_slist_append(headerlist, "Content-Type: application/x-protobuf");
//Set URL to recevie POST
curl_easy_setopt(curl, CURLOPT_VERBOSE, true);
curl_easy_setopt(curl, CURLOPT_POST, true);
curl_easy_setopt(curl, CURLOPT_HEADER, true);
curl_easy_setopt(curl, CURLOPT_URL, "http://localhost:9090/info");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
curl_global_cleanup();
}"
You should set the data pointer by curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);. In the meantime, you should set the data size by curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE_LARGE, length_of_data);.
You can find the libcurl post example in there.
And I copy the program below.
#include <stdio.h>
#include <curl/curl.h>
int main(void)
{
CURL *curl;
CURLcode res;
/* In windows, this will init the winsock stuff */
curl_global_init(CURL_GLOBAL_ALL);
/* get a curl handle */
curl = curl_easy_init();
if(curl) {
/* First set the URL that is about to receive our POST. This URL can
just as well be a https:// URL if that is what should receive the
data. */
curl_easy_setopt(curl, CURLOPT_URL, "http://postit.example.com/moo.cgi");
/* Now specify the POST data */
/* size of the POST data */
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE_LARGE, length_of_data);
/* binary data */
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return 0;
}
Fixing your original suggestion would probably make it something like this (based on the simplepost example from the libcurl web site):
#include <curl/curl.h>
int binarypost(char *binaryptr, long binarysize)
{
CURL *curl;
CURLcode res = CURLE_OK;
struct curl_slist *headerlist = NULL;
headerlist = curl_slist_append(headerlist, "Content-Type: application/x-protobuf");
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "http://localhost:9090/info");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, binaryptr);
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, binarysize);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
res = curl_easy_perform(curl);
/* always cleanup */
curl_easy_cleanup(curl);
}
return (int)res;
}

File downloaded by CURL in C++ cannot be opened

I'm working upon a program in C++, where I have to download a file using cURL, and further open it, but the problem is that when I try to open the file after downloading, it isn't opening. I'm trying to open an .exe file. Here's the portion of code, which is responsible for file download
curl = curl_easy_init();
if (curl) {
fp = fopen(outfilename.c_str(), "w");
curl_easy_setopt(curl, CURLOPT_URL, links[index]);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
fclose(fp);
}
and the line which should open the downloaded file :
ShellExecute(NULL, "open", fileToLaunch.c_str() , NULL, NULL, SW_HIDE);
When I try to launch manually the file (by clicking on it), Windows returns me an error message which says that respective app isn't a Win32 app. I'm using Visual Studio 2017.
Here's the whole chunk of code :
#include <stdio.h>
#include <curl/curl.h>
#include <curl/easy.h>
#include <string>
#include <iostream>
using namespace std;
size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {
size_t written = fwrite(ptr, size, nmemb, stream);
return written;
}
int main(void) {
CURL *curl;
FILE *fp;
CURLcode res;
string url = "Here goes the url for file download";
string outfilename = "C:\\1.exe";
curl = curl_easy_init();
if (curl) {
fp = fopen(outfilename.c_str(), "wb");
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
fclose(fp);
}
return 0;
ShellExecute(NULL, "open", outfilename.c_str(), NULL, NULL, SW_HIDE);
}
Change
fp = fopen(outfilename.c_str(), "w");
to
fp = fopen(outfilename.c_str(), "wb");
You're writing it to disk as text with newline translations by default. You need to write it as binary.
See https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/fopen-wfopen?view=vs-2017 for a fuller explanation.
First of all this line should be deleted :
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
because write_data function has the same functionality, as cURL's CURLOPT_WRITEDATA, and so, the line below the one which should be deleted will be enough, and secondly, on the line where curl_easy_setopt() is firstly met:
curl_easy_setopt(curl, CURLOPT_URL, url);
you should add to the url parameter .c_str(), so it will look like this :
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
because this function is unable to operate with string type of data...
And as Rob K mentioned, you have to change 'w' to 'wb' in fopen() function, because you are working with a binary stream of data.

Getting verbose information from libcurl to a file

I am trying to develop a QT Application in C/C++ which uses libcurl. Simply, i want to save VERBOSE data to a file. In libcurl API documentation, it is said that (https://curl.haxx.se/libcurl/c/CURLOPT_VERBOSE.html)
The verbose information will be sent to stderr, or the stream set with CURLOPT_STDERR.
So, VERBOSE information will be at stderr. And after i followed the link for CURLOPT_STDERR (https://curl.haxx.se/libcurl/c/CURLOPT_STDERR.html) which tells,
Pass a FILE * as parameter. Tell libcurl to use this stream instead of stderr when showing the progress meter and displaying CURLOPT_VERBOSE data.
At the CURLOPT_STDERR link, there exists a code sample. I've tried it on my own application as:
CURL *curl = curl_easy_init();
FILE *filep = fopen("dump.txt", "wb");
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, "https://www.google.com");
curl_easy_setopt(curl, CURLOPT_STDERR, filep);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_perform(curl);
}
CURLcode res = curl_easy_perform(curl);
if (CURLE_OK != res) {
fprintf(stderr, "curl told us %d\n", res);
}
curl_easy_cleanup(curl);
fclose(filep);
However, verbose information isn't shown at command line and the file which is created for the verbose information is empty. How can i solve this problem?
The following example works for me:
#include <stdio.h>
#include <curl/curl.h>
int main(int argc, char *argv[])
{
CURLcode ret;
CURL *hnd;
FILE* logfile;
logfile = fopen("dump.txt", "wb");
hnd = curl_easy_init();
curl_easy_setopt(hnd, CURLOPT_URL, "http://example.org");
curl_easy_setopt(hnd, CURLOPT_NOPROGRESS, 1L);
curl_easy_setopt(hnd, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(hnd, CURLOPT_STDERR, logfile);
ret = curl_easy_perform(hnd);
curl_easy_cleanup(hnd);
fclose(logfile);
return (int)ret;
}

cURL reconnect to the same url

When I'm trying to connect to a new url I need to run the program with the new url, the program fails to connect and when I restart the program it will connect.
So what I'm trying to do is reset, all connections and instead of restarting the program just try to connect to the url once and then reset the connection and reconnecting, causing the same effect as restarting the program.
Is there any way to do that? And what causes my program to not load a new url? And forcing me to restart the program to make it work?
Here is my code:
#include <iostream>
#include <curl/curl.h>
#include <string>
#include <fstream>
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)
{
std::ofstream file("source.txt");
CURL *curl;
CURLcode res = CURL_LAST;
std::string readBuffer;
curl = curl_easy_init();
while (curl && res != CURLE_OK){
curl_easy_setopt(curl, CURLOPT_URL, "http://www.example.com/");
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, true);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 5);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 5);
curl_easy_setopt(curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)");
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
curl_easy_setopt(curl, CURLOPT_VERBOSE, true);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
if (res != CURLE_OK)
curl = curl_easy_init();
}
file << readBuffer;
file.close();
std::cout << readBuffer << std::endl;
std::cin.ignore();
return 0;
}
Here I'm trying to use a while loop to reconnect and trying to use curl_easy_init() to reset the connection