title: POST Request in WinHttp c++
body: <p>I'm trying to make a POST request in c++ with the WinHTTP api <a href="https://msdn.microsoft.com/es-es/library/windows/desktop/aa384091(v=vs.85).aspx" rel="nofollow" title="More info Here">Click to the Microsoft Guide</a>, the problem is that the example that is available in the microsoft webpage is a "GET" request so I came up with this code searching on the internet:</p>
  
  <p>First we call the code:</p>
  
  <pre><code>HttpsWebRequestPost("example.com", "/api.php?action=UserLogin", "loginUsername=" + USERNAME + "&amp;loginPassword=" + PASSWORD + "&amp;url=/index.php?page=Portal");
  </code></pre>
  
  <p>Then:</p>
  
  <pre><code>#include &lt;Windows.h&gt;
  #include &lt;WinHttp.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;iostream&gt; //getchar
  #include &lt;fstream&gt;
  
  #pragma comment(lib, "winhttp.lib")
  
  using namespace std;
  
  std::wstring get_utf16(const std::string &amp;str, int codepage)
  {
      if (str.empty()) return std::wstring();
      int sz = MultiByteToWideChar(codepage, 0, &amp;str[0], (int)str.size(), 0, 0);
      std::wstring res(sz, 0);
      MultiByteToWideChar(codepage, 0, &amp;str[0], (int)str.size(), &amp;res[0], sz);
      return res;
  }
  
  string HttpsWebRequestPost(string domain, string url, string dat)
  {
      //Extra
      LPSTR  data = const_cast&lt;char *&gt;(dat.c_str());;
      DWORD data_len = strlen(data);
  
  
      wstring sdomain = get_utf16(domain, CP_UTF8);
      wstring surl = get_utf16(url, CP_UTF8);
      string response;
  
      DWORD dwSize = 0;
      DWORD dwDownloaded = 0;
      LPSTR pszOutBuffer;
      BOOL  bResults = FALSE;
      HINTERNET  hSession = NULL,
          hConnect = NULL,
          hRequest = NULL;
  
      // Use WinHttpOpen to obtain a session handle.
      hSession = WinHttpOpen(L"WinHTTP Example/1.0",
          WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
          WINHTTP_NO_PROXY_NAME,
          WINHTTP_NO_PROXY_BYPASS, 0);
  
      // Specify an HTTP server.
      if (hSession)
          hConnect = WinHttpConnect(hSession, sdomain.c_str(),
              INTERNET_DEFAULT_HTTP_PORT, 0);
  
      // Create an HTTP request handle.
      if (hConnect)
          hRequest = WinHttpOpenRequest(hConnect, L"POST", surl.c_str(),
              NULL, WINHTTP_NO_REFERER,
              WINHTTP_DEFAULT_ACCEPT_TYPES,
              0);
  
      // Send a request.
      if (hRequest)
          bResults = WinHttpSendRequest(hRequest,
              WINHTTP_NO_ADDITIONAL_HEADERS, 0,
              (LPVOID)data, data_len,
              data_len, 0);
  
  
      // End the request.
      if (bResults)
          bResults = WinHttpReceiveResponse(hRequest, NULL);
  
      // Keep checking for data until there is nothing left.
      if (bResults)
      {
          do
          {
              // Check for available data.
              dwSize = 0;
              if (!WinHttpQueryDataAvailable(hRequest, &amp;dwSize))
                  printf("Error %u in WinHttpQueryDataAvailable.\n",
                      GetLastError());
  
              // Allocate space for the buffer.
              pszOutBuffer = new char[dwSize + 1];
              if (!pszOutBuffer)
              {
                  printf("Out of memory\n");
                  dwSize = 0;
              }
              else
              {
                  // Read the data.
                  ZeroMemory(pszOutBuffer, dwSize + 1);
  
                  if (!WinHttpReadData(hRequest, (LPVOID)pszOutBuffer,
                      dwSize, &amp;dwDownloaded))
                      printf("Error %u in WinHttpReadData.\n", GetLastError());
                  else
                      //printf("%s", pszOutBuffer);
                      response = response + string(pszOutBuffer);
                  // Free the memory allocated to the buffer.
                  delete[] pszOutBuffer;
              }
          } while (dwSize &gt; 0);
      }
  
      // Report any errors.
      if (!bResults)
          printf("Error %d has occurred.\n", GetLastError());
  
      // Close any open handles.
      if (hRequest) WinHttpCloseHandle(hRequest);
      if (hConnect) WinHttpCloseHandle(hConnect);
      if (hSession) WinHttpCloseHandle(hSession);
  
      return response;
  
  }
  </code></pre>
  
  <p>But using WireShark I only get:</p>
  
  <pre><code>Hypertext Transfer Protocol
      POST ***************** HTTP/1.1\r\n
      Connection: Keep-Alive\r\n
      User-Agent: WinHTTP Example/1.0\r\n
      Content-Length: **\r\n
      Host: ******\r\n
      \r\n
  </code></pre>
  
  <p>Anyone can help meto fix this or know an easier method?
  Thanks</p>