Skip to content
Create a gist now

Instantly share code, notes, and snippets.

Embed URL


Subversion checkout URL

You can clone with
Download ZIP
A small tutorial depicting the use of curl libraries to upload a picture to twitpic with basic HTTP authentication
#include <iostream>
#include <sstream>
#include <curl/curl.h>
#define END_POINT ""
#define EXPECT_ARGS 4
size_t write_data(void *buffer, size_t size, size_t nmemb, void* userp)
std::stringstream strmResponse;
size_t nReal = size * nmemb;
strmResponse << std::string((char*)buffer, size*nmemb);
std::string sLine("");
while (getline(strmResponse, sLine)) {
std::cout << sLine.c_str() << std::endl;
return nReal;
int main (int argc, char * const argv[]) {
int result = 0;
CURL* hCurl = NULL;
CURLcode hResult;
std::string sUser("");
std::string sPass("");
std::string sFileName("");
//Curl for form data
struct curl_httppost *post = NULL;
struct curl_httppost *last = NULL;
if(argc < EXPECT_ARGS) {
std::cout << "Usage twitpic_upload twitterUserName twiiterPassword fileName" << std::endl;
throw false;
//Get the require params from command line args
sUser = argv[1];
sPass = argv[2];
sFileName = argv[3];
//Initialize curl, just don't let easy_init to do it for you
//Handle to the curl
hCurl = curl_easy_init();
if(NULL == hCurl) {
throw false;
//Construct the form
curl_formadd(&post, &last, CURLFORM_COPYNAME, "username", CURLFORM_COPYCONTENTS, sUser.c_str(), CURLFORM_END);
curl_formadd(&post, &last, CURLFORM_COPYNAME, "password", CURLFORM_COPYCONTENTS, sPass.c_str(), CURLFORM_END);
curl_formadd(&post, &last, CURLFORM_COPYNAME, "media", CURLFORM_FILE, sFileName.c_str(), CURLFORM_END);
//Specify the API Endpoint
hResult = curl_easy_setopt(hCurl,CURLOPT_URL, END_POINT);
//Specify the HTTP Method
hResult = curl_easy_setopt(hCurl, CURLOPT_HTTPPOST, post);
//Post Away !!!
hResult = curl_easy_perform(hCurl);
if(hResult != CURLE_OK){
std::cout << "Cannot find the twitpic site " << std::endl;
throw false;
catch (...) {
result = -1;
return result;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.