Last active
June 11, 2023 16:54
-
-
Save Kuranes/7503ce58e9ea0b899f17 to your computer and use it in GitHub Desktop.
simple https upload from c++ multi-plateform code using libcurl library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include <curl/curl.h> | |
struct MemoryStruct { | |
char *memory; | |
size_t size; | |
}; | |
static size_t | |
WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp) | |
{ | |
size_t realsize = size * nmemb; | |
struct MemoryStruct *mem = (struct MemoryStruct *)userp; | |
mem->memory = (char *)realloc(mem->memory, mem->size + realsize + 1); | |
if (mem->memory == NULL) { | |
/* out of memory! */ | |
printf("not enough memory (realloc returned NULL)\n"); | |
return 0; | |
} | |
memcpy(&(mem->memory[mem->size]), contents, realsize); | |
mem->size += realsize; | |
mem->memory[mem->size] = 0; | |
return realsize; | |
} | |
int main(int argc, char *argv[]) | |
{ | |
CURL *curl; | |
CURLcode res; | |
struct MemoryStruct chunk; | |
chunk.memory = (char*)malloc(1); /* will be grown as needed by the realloc above */ | |
chunk.size = 0; /* no data at this point */ | |
struct curl_httppost *formpost = NULL; | |
struct curl_httppost *lastptr = NULL; | |
struct curl_slist *headerlist = NULL; | |
static const char buf[] = "Expect:"; | |
curl_global_init(CURL_GLOBAL_ALL); | |
/* Fill in the file upload field */ | |
curl_formadd(&formpost, | |
&lastptr, | |
CURLFORM_COPYNAME, "fileModel", | |
CURLFORM_FILE, "f:\\tmp\\Dog_DemoHead.zip", | |
CURLFORM_END); | |
curl_formadd(&formpost, | |
&lastptr, | |
CURLFORM_COPYNAME, "fileNameModel", | |
CURLFORM_COPYCONTENTS, "f:\\tmp\\Dog_DemoHead.zip", | |
CURLFORM_END); | |
curl_formadd(&formpost, | |
&lastptr, | |
CURLFORM_COPYNAME, "token", | |
CURLFORM_COPYCONTENTS, "000000000000000000000000", | |
CURLFORM_END); | |
curl_formadd(&formpost, | |
&lastptr, | |
CURLFORM_COPYNAME, "title", | |
CURLFORM_COPYCONTENTS, "My title", | |
CURLFORM_END); | |
curl_formadd(&formpost, | |
&lastptr, | |
CURLFORM_COPYNAME, "description", | |
CURLFORM_COPYCONTENTS, "My description", | |
CURLFORM_END); | |
/* Fill in the submit field too, even if this is rarely needed */ | |
curl_formadd(&formpost, | |
&lastptr, | |
CURLFORM_COPYNAME, "submit", | |
CURLFORM_COPYCONTENTS, "send", | |
CURLFORM_END); | |
curl = curl_easy_init(); | |
if (curl) { | |
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, FALSE); | |
/* send all data to this function */ | |
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback); | |
/* we pass our 'chunk' struct to the callback function */ | |
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk); | |
/* some servers don't like requests that are made without a user-agent | |
field, so we provide curl */ | |
curl_easy_setopt(curl, CURLOPT_USERAGENT, "my3dsoftware"); | |
/* initalize custom header list (stating that Expect: 100-continue is not | |
wanted */ | |
headerlist = curl_slist_append(headerlist, buf); | |
/* what URL that receives this POST */ | |
//curl_easy_setopt(curl, CURLOPT_URL, "http://api.sketchfab.com/v1/models"); | |
curl_easy_setopt(curl, CURLOPT_URL, "https://api.sketchfab.com/v1/models"); | |
if ((argc == 2) && (!strcmp(argv[1], "noexpectheader"))) | |
/* only disable 100-continue header if explicitly requested */ | |
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist); | |
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost); | |
/* 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)); | |
} | |
else { | |
// printf("%lu bytes retrieved\n", (long)chunk.size); | |
printf("%s\n", chunk.memory); | |
} | |
/* always cleanup */ | |
curl_easy_cleanup(curl); | |
/* then cleanup the formpost chain */ | |
curl_formfree(formpost); | |
/* free slist */ | |
curl_slist_free_all(headerlist); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment