I want to make HTTPS PUT request to put a csv file. Below is the code used to upload data to Xively. Earlier I was getting 411 length required error. I referred to the code available here (Send string in PUT request with libcurl) to resolve this, where CURLOPT_CUSTOMREQUEST is made. Now I am getting HTTP 500 Internal Server error.
void upload()
{
CURL *curl;
CURLcode res;
FILE * hd_src;
struct stat file_info;
struct curl_slist* header = NULL;
char * csvfile = "123.csv";
/* get the file size */
stat(csvfile, &file_info);
hd_src = fopen("123.csv","rb");
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if(curl)
{
header = curl_slist_append(header,"X-ApiKey: 123123123"); /* API KEY HERE - sample only*/
header = curl_slist_append(header,"Accept: text/csv");
header = curl_slist_append(header, "Host: api.xively.com");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
/* Actual Xively feed is here. For demonstration purpose the feed is listed as 123 */
curl_easy_setopt(curl, CURLOPT_URL, "https://api.xively.com/v2/feeds/123.csv");
curl_easy_setopt(curl, CURLOPT_READDATA, hd_src);
curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)file_info.st_size);
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT" );
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, hd_src);
res = curl_easy_perform(curl);
if(res != CURLE_OK)
{
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
}
curl_easy_cleanup(curl);
}
fclose(hd_src);
curl_slist_free_all(header);
curl_global_cleanup();
}
Can anyone make suggestions? If I remove curl_easy_setopt(curl, CURLOPT_POSTFIELDS, hd_src), I'll get 411 length required error. I removed this and added Content-Length to header using file_info.st_size. Again 411 length required error is received. With curl_easy_setopt(curl, CURLOPT_POSTFIELDS, hd_src) 411 is solved but Xively gives 500 internal server error.
Thanks in advance.