title: Post a multipart/form-data HTTP request with WinHTTP
body: <p>I have been trying for the past few days to send an HTTP <code>POST</code> request to my SpringBoot application with the Win32 API, but I'm always receiving the same error. The request is a <code>multipart</code> consisting of a binary file and a JSON. Sending the request via Postman works with no problems, and I'm able to receive the file and the JSON correctly.</p>
  <p>I have looked at almost every post and question regarding how to construct an HTTP request with WinHTTP, and it seems they all do exactly what I did, but for some reason I'm getting a weirdly constructed request, as seen in the WireShark image below.</p>
  <p>It seems as if the request is not recognized as several parts, but rather as one chunk of data.</p>
  <p>Looking in WireShark at the correct request which was sent with postman, shows that the request consists of all parts, just as it supposed to.</p>
  <p>Here is my code:</p>
  <pre><code>LPCWSTR additionalHeaders = L&quot;Accept: application/json\r\nContent-Type: multipart/form-data; boundary=----------------------------346435246262465368257857\r\n&quot;;
  
  if (data-&gt;type == RequestType::MULTIPART) {
      size_t filesize = 0;
      char* fileData = fileToString(&quot;img.png&quot;, &quot;rb&quot;, &amp;filesize);
  
      WinHttpAddRequestHeaders(hRequest, additionalHeaders, -1L, WINHTTP_ADDREQ_FLAG_ADD);
        
      char postData1[] = 
          &quot;----------------------------346435246262465368257857\r\n&quot;
          &quot;Content-Disposition: form-data; name=\&quot;file\&quot;; filename=\&quot;img.png\&quot;\r\n&quot;
          &quot;Content-Type: image/png\r\n\r\n&quot;;  
      char postData2[] = 
          &quot;\r\n----------------------------346435246262465368257857\r\n&quot;
          &quot;Content-Disposition: form-data; name=\&quot;newData\&quot;\r\n&quot;
          &quot;Content-Type: application/json\r\n\r\n&quot;
          &quot;{\&quot;dataType\&quot;:\&quot;DEVICE_SETTINGS\&quot;}&quot;        
          &quot;\r\n----------------------------346435246262465368257857--\r\n&quot;;
  
       
      if (hRequest)
          bResults = WinHttpSendRequest(hRequest,
              WINHTTP_NO_ADDITIONAL_HEADERS,
              0, WINHTTP_NO_REQUEST_DATA, 0,
              lstrlenA(postData1) + lstrlenA(postData2) + filesize, NULL);
  
      DWORD dwBytesWritten = 0; 
      if (bResults)
          bResults = WinHttpWriteData(hRequest, postData1, lstrlenA(postData1), &amp;dwBytesWritten);
      if (bResults) 
          bResults = WinHttpWriteData(hRequest,(LPCVOID) fileData, filesize, &amp;dwBytesWritten);
      if (bResults)
          bResults = WinHttpWriteData(hRequest, postData2, lstrlenA(postData2), &amp;dwBytesWritten);
  }
  
  errorMessageID = ::GetLastError();
  
  // End the request.
  if (bResults)
      bResults = WinHttpReceiveResponse(hRequest, NULL);
  </code></pre>
  <p><a href="https://i.stack.imgur.com/rgrey.png" rel="nofollow noreferrer">Valid request sent with Postman</a></p>
  <p><a href="https://i.stack.imgur.com/5bIkD.png" rel="nofollow noreferrer">Invalid request sent with WinHTTP</a></p>
  <p><a href="https://i.stack.imgur.com/OOmGy.png" rel="nofollow noreferrer">Postman configuration 1</a></p>
  <p><a href="https://i.stack.imgur.com/suEZ2.png" rel="nofollow noreferrer">Postman configuration 2</a></p>
  <p><a href="https://i.stack.imgur.com/87ZPH.jpg" rel="nofollow noreferrer">SpringBoot controller</a></p>
  <p>Here is the error I receive in my SpringBoot app log:</p>
  <blockquote>
  <p>2022-03-04 14:48:34.520  WARN 25412 --- [nio-8010-exec-6] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.multipart.support.MissingServletRequestPartException: Required request part 'file' is not present]</p>
  </blockquote>
  <p>I would really appreciate any help here solving this mystery!</p>