C++ cURL Refresh Page - c++

I have the following code in C++
#include <cstdlib>
#include <iostream>
#include <curl/curl.h>
#include <string.h>
using namespace std;
int main(int argc, char *argv[])
{
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "http://example.com/mypage.html");
/* example.com is redirected, so we tell libcurl to follow redirection */
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
/* 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);
}
system("PAUSE");
return EXIT_SUCCESS;
}
When I'm running this program it is showing the source code of mypage.html on the console. Next I updated the source code of mypage.html and again executed the program, but it was printing the previous source code on the console again. Where's the problem? Please Help.

I faced the same problem recently. But it was with WinINet, not with cURL. Simply I changed my .html file to .php on my server and it worked fine! Actually .php files aren't cached by browser. So give it a try.

I think you rewrite source code of the page, but remember update page on web server or page cashed

Related

curl/curl.h library not found in dev c++ 6.3

`#include <stdio.h>
#include <curl/curl.h>
int main(void)
{
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl)
{
curl_easy_setopt(curl, CURLOPT_URL, "http://google.com");
/* example.com is redirected, so we tell libcurl to follow redirection */
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
/* 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);
}
return 0;
}`
i have linked libcurl.a file in the project and added inlude folder from the curl-7.87.0_1-win64-mingw folder, i have also copied the include in C:\Program Files (x86)\Embarcadero\Dev-Cpp\TDM-GCC-64\x86_64-w64-mingw32 folder and copied the libraries too (as suggested in a previous answer to a similar question here) but still it isnt working, the error im seeing is [Error] curl/curl.h: No such file or directory.
this is the code im trying to run

How can I set a var for the url in libcurl

I followed a tutorial to fetch a webpage. It worked but they manually set the URL.
I tried changing it to use a URL from a var, but that did not work.
I get an error in the terminal
"Couldn't resolve host name"
I tried main(char*) which gave the same error.
I can't seem to find anything online for this.
How can I make it so a user-defined var can be used as the URL?
code below.
#include <iostream>
#include <curl/curl.h>
#include <string>
using namespace std;
int main() {
string website;
getline(cin, website);
CURL* curl = curl_easy_init();
if (!curl) {
fprintf(stderr, "init failed\n");
return EXIT_FAILURE;
}
// set up
curl_easy_setopt(curl, CURLOPT_URL, "$website");
// perform
CURLcode result = curl_easy_perform(curl);
if (result != CURLE_OK) {
fprintf(stderr, "download prob: %s\n", curl_easy_strerror(result));
}
curl_easy_cleanup(curl);
return EXIT_SUCCESS;
}
"$website" is just a string, a piece of text. The variable should be referenced as website, and since the function is expecting a pointer to an array of characters, you use the c_str() or data() method of the class.
curl_easy_setopt(curl, CURLOPT_URL, website.data());

Downloading a file via curl in c++ from dropbox

I want to download a file from a dropbox shared link using curl in a c++ program
I found a dropbox api pdf that showed me how to do it
#include <stdio.h>
#include <curl/curl.h>
int main (int argc, char *argv[])
{
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) {
printf ("Running curl test.\n");
struct curl_slist *headers=NULL; /* init to NULL is important */
headers = curl_slist_append(headers, "Authorization: Bearer
<ACCESS_TOKEN>");
headers = curl_slist_append(headers, "Content-Type:");
headers = curl_slist_append(headers, "Dropbox-API-Arg:
{\"path\":\"/test.txt\"}");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_URL,
"https://content.dropboxapi.com/2/files/download");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "");
/* 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);
printf ("\nFinished curl test.\n");
}
curl_global_cleanup();
printf ("Done!\n");
return 0;
}
However, the comments supplied don't offer much explanation for me, and I can't get it to work.
I don't understand these three lines of code:
headers = curl_slist_append(headers, "Authorization: Bearer <ACCESS_TOKEN>");
headers = curl_slist_append(headers, "Content-Type:");
headers = curl_slist_append(headers, "Dropbox-API-Arg:{\"path\":\"/test.txt\"}");
I think I have to replace some stuff but I don't know what
"I think I have to replace some stuff but I don't know what" : Replace <ACCESS_TOKEN> with your actual access token.
You should also set the "Content-Type:" header to an appropriate value for the data you are fetching.
You must also change the value of the "Dropbox-API-Arg" header to match the file you are trying to get.
I finally found the solution to my problem.
Turns out I didn't have to use the Dropbox API
Here is the code
#include <iostream>
#include <curl/curl.h>
using namespace std;
size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream)
{
size_t written;
written = fwrite(ptr, size, nmemb, stream);
return written;
}
int main(int argc, char** argv) {
CURL *curl;
FILE *fp;
const char* destination = "D:\\Desktop\\test.exe";
fp = fopen(destination, "wb");
curl = curl_easy_init();
/* A long parameter set to 1 tells the library to follow any Location: header
* that the server sends as part of an HTTP header in a 3xx response. The
*Location: header can specify a relative or an absolute URL to follow.
*/
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_URL, "https://www.dropbox.com/s/09nd26tdyto23yz/BankAccount.exe?dl=1"); // "dl=0"changed to "dl=1" to force download
// disabe the SSL peer certificate verification allowing the program to download the file from dropbox shared link
// in case it is not used it displays an error message stating "SSL peer certificate or SSH remote key was not OK"
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
CURLcode res;
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
fclose(fp);
if (res ==CURLE_OK)
cout << "OK";
else
cout << curl_easy_strerror(res);
return 0;
}
Thanks you guys for trying to help me. I appreciate

expected primary-expression before "__typeof__"

I am new to c++. I am trying a tiny piece of code to interact with a local instance of InfluxDB.
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <curl/curl.h>
#include <curl/curlver.h>
#include <curl/easy.h>
#include <curl/mprintf.h>
#include <curl/multi.h>
#include <curl/stdcheaders.h>
#include <curl/system.h>
#include <curl/typecheck-gcc.h>
using namespace std;
bool createInfluxDB(char *url, char *data) {
CURL *curl;
curl = curl_easy_init();
if(curl) {
CURLcode res;
/* What Content-type should i use?*/
struct curl_slist* headers = curl_slist_append(headers, "Content-Type: application/json");
/*--data-urlencode*/
char *urlencoded = curl_easy_escape(curl, data, int(strlen(data)));
curl_easy_setopt(curl, CURLOPT_URL, url); // Error here
curl_easy_setopt(curl, CURLOPT_POST, 1L);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, urlencoded);
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)strlen(urlencoded));
res = curl_easy_perform(curl);
/*omitted controls*/
curl_free(urlencoded);
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
}
return(true);
}
int main(int argc, char *argv[]){
char *url = "http://localhost:8086/query";
char *data = "q=CREATE DATABASE mydb";
/* should i change data string to json?
data = "{\"q\":\"CREATE DATABASE mydb\" }" */
bool res = createInfluxDB(url, data);
/*control result*/
return(0);
}
When trying to build it, i get the following errors:
expected primary-expression before '__typeof__'
extend list of errors
Any ideas where it might come from ?
I am working on codeblocks 16.01, using GCC compiler on Ubuntu.
That was due to curl library not being linked to the compiler. The correct way to fix this in Codeblocks editor:
Project>Build options..>"Linker settings" tab
Under Link libraries, click on add, type the library name on the popping window, click OK, click OK again. And reboot Codeblocks.

C++ - cURL - Gettings 501 error - Why isn't the server supporting POST

I am very new to cURL and was modifying the following simple cURL example to work with my needs:
https://curl.haxx.se/libcurl/c/simple.html
For the simple.cpp:
It works, I have cURL set up correctly and I see the html of www.example.com
For the http-post.cpp
#include <stdio.h>
#include <curl/curl.h>
int main(void)
{
CURL *curl;
CURLcode res;
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://127.0.0.1:3000");
/* Now specify the POST data */
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "name=daniel&project=curl");
/* 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);
}
return 0;
}
the following error comes up:
<head>
<title>Error response</title>
</head>
<body>
<h1>Error response</h1>
<p>Error code 501.
<p>Message: Unsupported method ('POST').
<p>Error code explanation: **501 = Server does not support this operation.**
</body>
I am using Eclipse to write and test my code, and in the settings I have the following in my settings, so I know I thought I was setup correctly.
Linker Libraries