title: Uploading a file to http(s) server using POST request through libcurl in c
body: <p>i want to upload a file to http server with post request, for this i am using libcurl.</p>
  
  <pre><code>static int copy_to_http_server(char *src, char *disp_src, char srcflag,
                                   char *dst, char *disp_dst, char dstflag)
  {
    CURL *curl;
    CURLcode res;
    char *url = disp_dst;
    char *outfilename = src;
  
    struct curl_httppost *formpost=NULL;
    struct curl_httppost *lastptr=NULL;
    struct curl_slist *headerlist=NULL;
    static const char buf[] = "Expect:";
  
    /* src = /users/play/team.jpg 
     * url = http://10.1.2.3/repo/team_server.jpg 
     * i want 'src' to be copied under 'repo' directory on http server and 
     * the file name on the server should be team_server.jpg or team.jpg is
     * also fine */
  
    curl_global_init(CURL_GLOBAL_ALL);
  
    /* Fill in the file upload field */
    curl_formadd(&amp;formpost,
                 &amp;lastptr,
                 CURLFORM_COPYNAME, "sendfile",
                 CURLFORM_FILE, src,
                 CURLFORM_END);
  
    /* Fill in the filename field */
    curl_formadd(&amp;formpost,
                 &amp;lastptr,
                 CURLFORM_COPYNAME, "filename",
                 CURLFORM_COPYCONTENTS, src,
                 CURLFORM_END);
    /* Fill in the submit field too, even if this is rarely needed */
    curl_formadd(&amp;formpost,
               &amp;lastptr,
               CURLFORM_COPYNAME, "submit",
               CURLFORM_COPYCONTENTS, "send",
               CURLFORM_END);
  
    curl = curl_easy_init();
    /* initialize custom header list (stating that Expect: 100-continue is     not
       wanted */
    if(curl) {
      curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
      /* what URL that receives this POST */
      curl_easy_setopt(curl, CURLOPT_URL, url);
      if (res != CURLE_OK) {
         printf("could not set url : %s\n",curl_easy_strerror(res));
      } else {
        printf("able to set\n");
      }  
      curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
      curl_easy_setopt(curl, CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);
      curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
  
      /* Perform the request, res will get the return code */
          printf("calling curl_easy_perform\n");
      res = curl_easy_perform(curl);
      /* Check for errors */
      if(res != CURLE_OK){
        fprintf(stderr, "curl_easy_perform() failed: %s\n",
                curl_easy_strerror(res));
      }
          printf("after calling curl_easy_perform\n");
      /* always cleanup */
      curl_easy_cleanup(curl);
  
      /* then cleanup the formpost chain */
      curl_formfree(formpost);
    }
    curl_global_cleanup();
    return 1;
  }
  </code></pre>
  
  <p>below is the verbose output that i am getting as post response :-</p>
  
  <pre><code>&gt; POST /repo/proc.out HTTP/1.1
  Host: 10.1.2.3
  Accept: */*
  Content-Length: 0
  Expect: 100-continue
  Content-Type: multipart/form-data; boundary=----------------------------8708d4558a12
  
  &lt; HTTP/1.1 404 Not Found
  &lt; Date: Sat, 28 May 2016 07:37:10 GMT
  * Server Apache/2.2.15 (CentOS) is not blacklisted
  &lt; Server: Apache/2.2.15 (CentOS)
  &lt; Content-Length: 289
  &lt; Connection: close
  &lt; Content-Type: text/html; charset=iso-8859-1
  &lt; 
  &lt;!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"&gt;
  &lt;html&gt;&lt;head&gt;
  &lt;title&gt;404 Not Found&lt;/title&gt;
  &lt;/head&gt;&lt;body&gt;
  &lt;h1&gt;Not Found&lt;/h1&gt;
  &lt;p&gt;The requested URL /repo/proc.out was not found on this server.&lt;/p&gt;
  &lt;hr&gt;
  &lt;address&gt;Apache/2.2.15 (CentOS) Server at 10.1.2.3 Port 80&lt;/address&gt;
  &lt;/body&gt;&lt;/html&gt;
  * Closing connection 0
  </code></pre>
  
  <p>Most amazing part is i am not able to upload the file even with command line , and getting same error :-
    curl -F filedata=@myfile.txt <a href="http://10.1.2.3/repo" rel="nofollow">http://10.1.2.3/repo</a></p>
  
  <pre><code>&lt;!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"&gt;
  &lt;html&gt;&lt;head&gt;
  &lt;title&gt;404 Not Found&lt;/title&gt;
  &lt;/head&gt;&lt;body&gt;
  &lt;h1&gt;Not Found&lt;/h1&gt;
  &lt;p&gt;The requested URL /repo/ was not found on this server.&lt;/p&gt;
  &lt;hr&gt;
  &lt;address&gt;Apache/2.2.15 (CentOS) Server at 10.1.2.3 Port 80&lt;/address&gt;
  &lt;/body&gt;&lt;/html&gt;
  </code></pre>
  
  <p>but i am able to download a file from the same path i.e "<a href="http://10.1.2.3/repo/available_file.jpeg" rel="nofollow">http://10.1.2.3/repo/available_file.jpeg</a>"</p>
  
  <p>do i need anything extra to configure on http server or any script/programm should be running on the server to upload  a file to server ?</p>
  
  <p>could any one please help ?</p>