Note that there are some explanatory texts on larger screens.

plurals
  1. POUpload file using libcurl + POST and headers
    text
    copied!<p>I am trying to upload a file to a webserver using libcurl (in C). The server requires that I use POST, not PUT (which is what CURLOPT_UPLOAD would use). I also need to send a specific token in the header. This is what I have so far:</p> <pre><code>int upload(char *filepath, char *filename, char *token) { CURL *curl; CURLcode res; FILE *fd; struct stat file_info; char *file; asprintf(&amp;file, "%s/%s", filepath, filename); fd = fopen(file, "rb"); if (!fd) { fprintf(stderr, "Could not open file.\n"); return 1; } if (fstat(fileno(fd), &amp;file_info) != 0) { fprintf(stderr, "Could not get file information.\n"); return 1; } curl = curl_easy_init() curl_easy_setopt(curl, CURLOPT_URL, "https://127.0.0.1/upload"); curl_easy_setopt(curl, CURLOPT_READDATA, fd); curl_easy_setopt(curl, CURLOPT_POST, 1L); struct curl_slist *headers = NULL; headers = curl_slist_append(headers, "Content-Type:application/octet-stream"); char *my_token = malloc(snprintf(NULL, 0, "X-Token:%s", token) + 1); sprintf(my_token, "X-Token:%s", token); headers = curl_slist_append(headers, my_token); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); res = curl_easy_perform(curl); if (res != CURLE_OK) { fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); return 1; } fprintf(stdout, "Upload OK.\n"); return 0; } </code></pre> <p>The only response I get from the server is that it is a "400 Bad Request".</p> <p>I have written code in Python that performs the same action, and this file upload works:</p> <pre><code>import urllib, urllib2, json, os def upload(self, filepath, filename, token): # Open file f = open(os.path.join(filepath, filename), 'rb') filedata = f.read() f.close() request = urllib2.Request("https://127.0.0.1/upload" % filedata, {"Content-Type":"application/octet-stream", "X-Token":token}) res = self.opener.open(request) return json.loads(res.read()) </code></pre> <p>UPDATE: After running with verbose mode:</p> <pre><code>&gt; POST /upload HTTP/1.1 Host: 127.0.0.1 Accept: */* Content-Type:application/octet-stream X-Token: 17f684-b98c Content-Length: -1 Expect: 100-continue &lt; HTTP/1.1 Bad Request .... </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload