title: HTTP Post Header Fields with libcurl
body: <p>First of, I'm very new to HTTP commands and the libcurl library, so there is a good chance that I'm not understanding something fundamental. That said, I'm attempting to replicate an HTTP POST command sent to a device via an internal server on a windows based MFC app. Essentially I'm sending a small bitmap image along with a command. I captured the command using Fiddler, and it looks something like:</p>
  
  <pre><code>POST /Service/MyCommand HTTP/1.1
  Authorization: MyAuth
  
  Content-Type: image/bmp
  User-Agent: Mozilla/4.0 (Windows 7 6.1) Java/1.7.0_51
  Host: MyHost:MyPort
  Accept: MyAccept
  Connection: MyConnection
  Content-Length: 15606
  
  /* BMP Data */
  </code></pre>
  
  <p>I'm having two problems replicating this (using libcurl). First, my post command of 'Service/MyCommand' appears at the very end of the header, rather than after the 'POST /'. I have attempted to move it around, but it will cease to appear on my WireShark filter window. Second, when I attempt to set my content length to 15606, as with the original, the protocol on WireShark switches from "POST" to "TCP". I've attached the code below.</p>
  
  <pre><code>int CHttpPost::fnSendContent()
  {
  using namespace std;
  int Error = 0;
  CString str;
  
  CURL* curl;
  CURLcode res;
  struct curl_httppost *formpost=NULL;
  struct curl_httppost *lastptr=NULL;
  struct curl_slist *headerlist=NULL;
  static const char buf[] = "a"; // not sure what to do with this
  
  curl_global_init(CURL_GLOBAL_ALL);
  
  curl_formadd(&amp;formpost, &amp;lastptr, CURLFORM_COPYNAME, "importfile", CURLFORM_FILE, "MyImage.bmp", CURLFORM_CONTENTTYPE, "image/bmp", CURLFORM_END);
  curl_formadd(&amp;formpost, &amp;lastptr, CURLFORM_COPYNAME, "action", CURLFORM_COPYCONTENTS, "upload", CURLFORM_END);
  
  curl = curl_easy_init();
  headerlist = curl_slist_append(headerlist, buf);
  headerlist = curl_slist_append(headerlist, "Authorization: MyAuth");
  headerlist = curl_slist_append(headerlist, "Content-Type: image/bmp");
  headerlist = curl_slist_append(headerlist, "User-Agent: Mozilla/4.0 (Windows 7 6.1) Java/1.7.0_51");
  headerlist = curl_slist_append(headerlist, "Accept: MyAccept");
  headerlist = curl_slist_append(headerlist, "Connection: MyConnection");
  headerlist = curl_slist_append(headerlist, "Content-Length: 15606");
  
  
  //Set URL to recevie POST
  curl_easy_setopt(curl, CURLOPT_VERBOSE, true);
  curl_easy_setopt(curl,CURLOPT_POST, true);
  curl_easy_setopt(curl, CURLOPT_HEADER, true);
  curl_easy_setopt(curl, CURLOPT_URL, "MyHost:MyPort");
  curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
  curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "Service/MyCommand");
  curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
  
  
  res = curl_easy_perform(curl);
  
  curl_easy_cleanup(curl);
  curl_global_cleanup();
  
  return Error;
  }
  </code></pre>
  
  <p>Any other suggestions or corrections you have are much appreciated as well.</p>
  
  <p>EDIT: I was being an idiot by placing the "Service/MyCommand" in the postfield rather than the URL. I apparently misunderstood one of the tutorials. My content-length question still stands however.</p>