Skip to content

Instantly share code, notes, and snippets.

@ajmontag
Last active December 14, 2015 03:38
Show Gist options
  • Save ajmontag/5021961 to your computer and use it in GitHub Desktop.
Save ajmontag/5021961 to your computer and use it in GitHub Desktop.
POST to Google App Engine Blobstore from CURL easy interface
void postBlob(const std::string& uploadurl, const std::string& blobstring) {
CURL *easyhandle;
CURLcode res;
easyhandle = curl_easy_init();
if (!easyhandle) {
throw std::runtime_error("Unable to init curl handle");
}
curl_easy_setopt(easyhandle, CURLOPT_URL, uploadurl.c_str());
struct curl_httppost *post = NULL;
struct curl_httppost *last = NULL;
curl_formadd(&post,
&last,
CURLFORM_PTRNAME,
"formname", // seems to be unimportant
CURLFORM_BUFFER,
"thefilename", // the filename you see in the blobstore viewer
CURLFORM_BUFFERPTR,
blobstring.c_str(),
CURLFORM_BUFFERLENGTH,
blobstring.size(),
CURLFORM_CONTENTTYPE,
"text/plain", // set your MIME type
CURLFORM_END);
// Set the form info
curl_easy_setopt(easyhandle, CURLOPT_HTTPPOST, post);
// do the post
res = curl_easy_perform(easyhandle);
curl_easy_getinfo(easyhandle, CURLINFO_RESPONSE_CODE, &responseCode_);
// free the post data
curl_formfree(post);
if(!easyhandle || res != CURLE_OK) {
std::err << "Error posting logs: " << curl_easy_strerror(res) << std::endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment