title: How do I use the winhttp api with "transfer-encoding: chunked"
body: <p>I'm trying to send some data to a web service which requires the "Transfer-encoding: chunked" header. It works fine with a normal POST request.
  But as soon as I add the header, I always get:</p>
  
  <blockquote>
    <p>The content could not be delivered due to the following condition:
    Received invalid request from client</p>
  </blockquote>
  
  <p>This is the part where the request is sent:</p>
  
  <pre><code>std::vector&lt;std::wstring&gt; m_headers;
  m_headers.push_back(TEXT("Transfer-encoding: chunked"));
  std::wstring m_verb(TEXT("POST"));
  std::vector&lt;unsigned __int8&gt; m_payload;
  
  HINTERNET m_connectionHandle = WinHttpConnect(m_http-&gt;getSessionHandle(), hostName.c_str(), m_urlParts.nPort, 0);
  if (!m_connectionHandle) {
      std::cout &lt;&lt; "InternetConnect failed: " &lt;&lt; GetLastError() &lt;&lt; std::endl;
      return;
  }
  
  __int32 requestFlags = WINHTTP_FLAG_SECURE | WINHTTP_FLAG_REFRESH;
  HINTERNET m_requestHandle = WinHttpOpenRequest(m_connectionHandle, m_verb.c_str(), (path + extra).c_str(), NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, requestFlags);
  if(!m_requestHandle) {
      std::cout &lt;&lt; "HttpOpenRequest failed: " &lt;&lt; GetLastError() &lt;&lt; std::endl;
      return;
  }
  
  for(auto header : m_headers) {
      if(!WinHttpAddRequestHeaders(m_requestHandle, (header + TEXT("\r\n")).c_str(), -1, WINHTTP_ADDREQ_FLAG_ADD)) {
          std::cout &lt;&lt; "WinHttpAddRequestHeaders failed: " &lt;&lt; GetLastError() &lt;&lt; std::endl;
          return;
      }
  }
  
  if(!WinHttpSendRequest(m_requestHandle, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, WINHTTP_IGNORE_REQUEST_TOTAL_LENGTH, (DWORD_PTR)this)) {
      std::cout &lt;&lt; "HttpSendRequest failed: " &lt;&lt; GetLastError() &lt;&lt; std::endl;
      return;
  }
  
  unsigned chunkSize = 1024;
  unsigned chunkCount = m_payload.size() / chunkSize;
  char chunksizeString[128];
  for (unsigned i = 0; i &lt;= chunkCount; i++) {
      unsigned actualChunkSize = std::min&lt;unsigned&gt;(chunkSize, m_payload.size() - i * chunkSize);
      sprintf_s(chunksizeString, "%d\r\n", actualChunkSize);
      if (!WinHttpWriteData(m_requestHandle, chunksizeString, strlen(chunksizeString), (LPDWORD)&amp;m_totalBytesWritten)) {
          std::cout &lt;&lt; "HttpWriteData failed: " &lt;&lt; GetLastError() &lt;&lt; std::endl;
          return;
      }
      if (!WinHttpWriteData(m_requestHandle, m_payload.data() + i * chunkSize, actualChunkSize, (LPDWORD)&amp;m_totalBytesWritten)) {
          std::cout &lt;&lt; "HttpWriteData failed: " &lt;&lt; GetLastError() &lt;&lt; std::endl;
          return;
      }
  }
  
  // terminate chunked transfer
  if (!WinHttpWriteData(m_requestHandle, "0\r\n", strlen("0\r\n"), (LPDWORD)&amp;m_totalBytesWritten)) {
      std::cout &lt;&lt; "HttpWriteData failed: " &lt;&lt; GetLastError() &lt;&lt; std::endl;
      return;
  }
  
  if(!WinHttpReceiveResponse(m_requestHandle, NULL)) {
      std::wcout &lt;&lt; "HttpReceiveResponse failed: " &lt;&lt; GetLastError() &lt;&lt; std::endl;
      return;
  }
  </code></pre>
  
  <p>I had to copy it from different files, so I hope I got all the important variable definitions. Right now I only use it synchronously since I thought it easier to debug.</p>
  
  <p>As it works with normal POST requests (where I just use WinHttpSendRequest with the payload) I'm guessing it must have to do with the way I use WinHttpSendRequest &amp; WinHttpWriteData, I just don't see how else it should be used.</p>
  
  <p>Any help is appreciated!</p>