Using cUrl ( windows ) and was trying to get an upload function to work. However, it just doesnt seem to work. Getting error ERROR_FILE_NOT_PROVIDED from anonfiles
Here is the API = https://anonfiles.com/docs/api
Here is my code:
std::string UploadPicture()
{
CURL *curl;
CURLcode res;
std::string readBuffer;
curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(curl, CURLOPT_URL, "https://api.anonfiles.com/upload");
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "cache-control: no-cache");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "file=test.ini");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
return readBuffer;
}
return "Failed";
}
Any help is welcome. I have tried many other ways as well, but didnt manage to get it right.
Related
im trying to send an email with curl and c++ just like this example but when i execute the program:
#include <iostream>
#include <curl/curl.h>
int main()
{
int a;
char errbuf[CURL_ERROR_SIZE] = {0};
CURL *curl = curl_easy_init();
CURLcode res;
struct upload_status upload_ctx = { 0 };
if(curl)
{
curl_easy_setopt(curl, CURLOPT_URL, "smtps://smtp.gmail.com:465");
curl_easy_setopt(curl, CURLOPT_MAIL_FROM, "");
curl_easy_setopt(curl, CURLOPT_PASSWORD, "");
curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, "");
curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source);
curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx);
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
curl_easy_setopt(curl, CURLOPT_CAINFO, "D:\\MinGW\\cacert.pem");
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
check_errors(res, errbuf);
return 0;
}
i get this error:
curl_easy_perform() failed: Failed sending data to the peer
libcurl: (55) MAIL failed: 530
I deleted most of the code because i couldn't post the question but the rest is in the example.
The problem i had in my code was that i thought that by using:
curl_easy_setopt(curl, CURLOPT_MAIL_FROM, "myemail#example.com"); i was specifyng the username, which was in fact, an error.
To specify the username libcurl has another option:
curl_easy_setopt(curl, CURLOPT_USERNAME, "myemail#example.com");
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.
I'm trying to upload pictures using c++ application with libcurl to my server, the file in my server is a protected directory, I tried everything I know but it didn't work.
struct curl_slist *headerlist = NULL;
const char * Picture = "C:\xxxx";
FILE *fd = fopen(Picture, "rb");
struct stat file_info;
fstat(fileno(fd), &file_info);
headerlist = curl_slist_append(headerlist, "user : xxxx");
headerlist = curl_slist_append(headerlist, "password : xxxxx");
headerlist = curl_slist_append(headerlist, "Content-Type: image/jpeg");
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, "http://xxxx.000webhostapp.com/Pictures");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
curl_easy_setopt(curl, CURLOPT_POST, 1L);
curl_easy_setopt(curl, CURLOPT_READDATA, fd);
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (curl_off_t)file_info.st_size);
res = curl_easy_perform(curl);
if (res != CURLE_OK)
{
MessageBox::Show("Picture Failed");
}
curl_easy_cleanup(curl);
curl_global_cleanup();
user and password are not HTTP headers, so you should not be passing them via CURLOPT_HTTPHEADER. Use CURLOPT_USERNAME and CURLOPT_PASSWORD instead. Also look at CURLOPT_HTTPAUTH.
Try this:
curl_global_init(CURL_GLOBAL_ALL);
...
const char *Picture = "C:\\xxxx";
FILE *fd = fopen(Picture, "rb");
if (!fd)
{
MessageBox::Show("Cannot Open Picture File");
}
else
{
struct stat file_info;
fstat(fileno(fd), &file_info);
curl = curl_easy_init();
if (!curl)
{
MessageBox::Show("Cannot Initialize CURL Session");
}
else
{
struct curl_slist *headerlist = curl_slist_append(NULL, "Content-Type: image/jpeg");
curl_easy_setopt(curl, CURLOPT_URL, "http://xxxx.000webhostapp.com/Pictures");
curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_easy_setopt(curl, CURLOPT_USERNAME, "xxxx");
curl_easy_setopt(curl, CURLOPT_PASSWORD, "xxxxx");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
curl_easy_setopt(curl, CURLOPT_POST, 1L);
curl_easy_setopt(curl, CURLOPT_READDATA, fd);
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (curl_off_t)file_info.st_size);
res = curl_easy_perform(curl);
if (res != CURLE_OK)
{
MessageBox::Show("Picture Failed");
}
curl_easy_cleanup(curl);
}
fclose(fd);
}
...
curl_global_cleanup();
I am wondering how can I add a progress bar for curl, below is my code, it works 100% I just want to know how to add a progress bar (C++) maybe like 10% > 30% > 100% (WINDOWS OS)
CURL *curl;
FILE *fp;
CURLcode res;
char *url= "http://ipv4.download.thinkbroadband.com/5MB.zip";
char outfilename[FILENAME_MAX] = "./ATNA/5MB.zip";
curl_version_info_data * vinfo = curl_version_info(CURLVERSION_NOW);
curl = curl_easy_init();
if (curl) {
fp = fopen(outfilename,"wb");
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_CAINFO, "./ca-bundle.crt");
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
I have the following curl function which executes in a loop:
curl = curl_easy_init();
if (curl) {
CurlResponse = "";
host = "http://exaple.com";
LibcurlHeaders = curl_slist_append(LibcurlHeaders, "Expect:");
curl_easy_setopt(curl, CURLOPT_URL, (host).c_str());
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER , 1);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST , 1);
curl_easy_setopt(curl, CURLOPT_CAINFO, SSLPath.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, LibcurlHeaders);
curl_easy_setopt(curl, CURLOPT_VERBOSE, CurlVerbose);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, CurlPostData.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, LibcurlResponse);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &CurlResponse);
res = curl_easy_perform(curl);
curl_slist_free_all(LibcurlHeaders); <----------
if (res != CURLE_OK) {
LibcurlError(curl_easy_strerror(res), host);
}
curl_easy_cleanup(curl);
}
curl_global_cleanup();
Everything works fine when I remove the line:
curl_slist_free_all(LibcurlHeaders);
However, in the Libcurl Docs it shows to use it like I do. At least thats how I understand it...
So what am I doing wrong and/or missing?
Thanks for your answers
* EDIT *
So, basically:
LibcurlHeaders = null:
Curl call with headers
// cant clear headers
Curl call with 2 headers
// can't clear headers
Curl call with 3 headers
// can't clear headers
What I want is that the headers used in the curl call are cleared when the curl call is done so I get:
LibcurlHeaders = null:
Curl call with headers
Headers cleared
Curl call with headers
Headers cleared
Curl call with headers
Headers cleared
You said in comments that you are calling curl in a loop. Your example does not show that. But assuming the code you did show is inside of larger code that is actually in a loop, you just need to make sure that your LibcurlHeaders variable is NULL before calling curl_slist_append() for the first time of each new HTTP request, eg:
curl = curl_easy_init();
if (curl) {
CurlResponse = "";
host = "http://exaple.com";
LibcurlHeaders = NULL; // <-- HERE
LibcurlHeaders = curl_slist_append(LibcurlHeaders, "Expect:");
/* alternatively, and ultimately safer and more accurate:
LibcurlHeaders = curl_slist_append(NULL, "Expect:");
*/
curl_easy_setopt(curl, CURLOPT_URL, host.c_str());
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 1);
curl_easy_setopt(curl, CURLOPT_CAINFO, SSLPath.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, LibcurlHeaders);
curl_easy_setopt(curl, CURLOPT_VERBOSE, CurlVerbose);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, CurlPostData.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, LibcurlResponse);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &CurlResponse);
res = curl_easy_perform(curl);
curl_slist_free_all(LibcurlHeaders);
LibcurlHeaders = NULL; // <-- FOR GOOD MEASURE!
if (res != CURLE_OK) {
LibcurlError(curl_easy_strerror(res), host);
}
curl_easy_cleanup(curl);
}
Wauw,
I managed to fix the issue. It seems to be a bug with Libcurl which can be fixed rather easily.
The problem was that I declared struct curl_slist *LibcurlHeaders=NULL; globally.
When I declared struct curl_slist *LibcurlHeaders=NULL; just before each curl_easy_init(); call and removed the global declaration, the curl_slist_free_all(LibcurlHeaders); worked and didn't crash my program anymore!
Short:
The problem was the global declaration of struct curl_slist *LibcurlHeaders=NULL; which had to be declared in front of each curl_easy_init(); function.