cURL / Mailing Lists / curl-library / Single Mail

curl-library

Unable to send the image file.

From: Anil J <anilj.mailing_at_gmail.com>
Date: Tue, 16 Apr 2013 07:17:33 -0400

Hello List,

When used the below program (httpclient.c) to send a image file to the
server, server is able to save, but the file is an invalid image file.
However when I send the same file from command line (see below), the server
is able to save and file is also able to open in the image viewer.

curl -i --request POST --data-binary "@image.jpeg"
http://localhost:8085/HelloWorldServlet/HelloWorld

Can you please let me know what's wrong with the program I've written.

====== libCurl API based Program ========

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <curl/curl.h>

int main(int argc, char *argv[])
{
  CURL *curl;
  CURLcode res;
  int i = 0;
  int fileLen = 0;
  FILE *fp;
  char* filename = "image.jpeg";
  char *buffer;
  char buf[9];
  char contentLen[100];

  if (argc < 2) {
    printf("Not enough arumen spsecified. Please specify the URL\n");
    return 0;
  }

  /* Read the image file, and post it to the Servlet for processing. */
  fp = fopen(filename, "rb");

  if(fp == NULL) {
    printf("file not found!\n");
  } else {
    fseek(fp,0L,SEEK_END);
    fileLen = ftell(fp);
    printf("the file's length is %1d Bytes\n",fileLen);
    fclose(fp);
  }

  /* Prepare the buffer to send to the server.*/
  buffer = (char *)malloc(fileLen + 1);
  if (!buffer) {
    fprintf(stderr, "Memory error!");
    fclose(fp);
    return;
  }
  //Read file contents into buffer
  fread(buffer, fileLen, 1, fp);
  fclose(fp);
  snprintf(buf, 10,"%d",fileLen);
  strcpy(contentLen, "Content-Length: ");
  strcpy(contentLen, buf);

  struct curl_slist *headers=NULL;
  headers = curl_slist_append(headers, "Content-Type: image/jpeg");
  headers = curl_slist_append(headers, contentLen);
  headers = curl_slist_append(headers, "Custom-Header: custom1");

  curl = curl_easy_init();

  if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, argv[1]);

    /* post binary data */
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, buffer);

    /* set the size of the postfields data */
    curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, fileLen);

    /* pass our list of custom made headers */
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);

    /* example.com is redirected, so we tell libcurl to follow redirection
*/
    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);

    /* 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));
    }

    /* always cleanup */
    curl_easy_cleanup(curl);

    /* free the header list */
    curl_slist_free_all(headers);
  }

  free(buffer);
  return 0;
}

-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2013-04-16