Search code examples
c++winhttp

Posting form using WinHttp


Do i need to add any headers before making a post to server?

For example, Currently I'm trying to send a request along with the post data this way,

  LPCWSTR post = L"name=User&subject=Hi&message=Hi";

    if (!(WinHttpSendRequest( hRequest, 
                            WINHTTP_NO_ADDITIONAL_HEADERS,
                            0, (LPVOID)post, wcslen(post), 
                            wcslen(post), 0)))
    {
          //error
    }

should this work?


Solution

  • I'd guess

    • you need to pass narrow strings not wide as the post data. I don't know if you're specifying a content type for the posted data which would specify the encoding - you probably should if it's easy to - or just re-encode the string as UTF-8, or just assemble as a narrow string in the first place
    • you might need an explicit end-of-line on the post data, i.e. add \r\n to your (narrow) string - I don't know if the API's going to add one since I assume you'd make the same call for binary data.