Skip to content

Instantly share code, notes, and snippets.

@chchwy
Forked from creativepsyco/upload.cpp
Created September 20, 2017 02:10
Show Gist options
  • Save chchwy/ffc7dddc8a5476deff6aae45efb9f7dc to your computer and use it in GitHub Desktop.
Save chchwy/ffc7dddc8a5476deff6aae45efb9f7dc to your computer and use it in GitHub Desktop.
Upload File + Callback
#include <stdio.h>
#include <iostream>
#include <string>
#include <curl/curl.h>
using namespace std;
static string data;
size_t writeCallback(char *buf, size_t size, size_t nmemb, void *up)
{
//callback must have this declaration
//buf is a pointer to the data that curl has for us
//size*nmemb is the size of the buffer
for (int c = 0; c < size * nmemb; c++)
{
// This is data that is coming
cout << buf[c];
data.push_back(buf[c]);
}
// this is the data that will come
cout << "data \n" << data << endl;
return size * nmemb; //tell curl how many bytes we handled
}
int main(int argc, char *argv[])
{
data = "";
struct curl_httppost *post = NULL;
struct curl_httppost *last = NULL;
CURLcode res;
CURL *curl;
curl = curl_easy_init();
if (curl)
{
curl_formadd(&post, &last,
CURLFORM_COPYNAME, "image",
CURLFORM_FILE, "/Users/msk/Desktop/36986233436.jpg",
CURLFORM_END);
curl_formadd(&post, &last,
CURLFORM_COPYNAME, "image",
CURLFORM_COPYCONTENTS, "/Users/msk/Desktop/36986233436.jpg",
CURLFORM_END);
/////////////////////////////////////////////////////////////////////
curl_formadd(&post, &last,
CURLFORM_COPYNAME, "key",
CURLFORM_COPYCONTENTS, "1748ee815be8f13cea057a29a7ec47ee",
CURLFORM_END);
curl_easy_setopt(curl, CURLOPT_URL, "http://api.imgur.com/2/upload.xml");
curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &writeCallback);
//curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); //tell curl to output its progress
res = curl_easy_perform(curl);
if (res)
{
return 0;
}
curl_formfree(post);
}
else
{
return 0;
}
curl_easy_cleanup(curl);
// fprintf(stderr, "%s\n", &data);
//cout << data << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment