locked
Post Request Content Type

    Question

  • I need to send a post request that has content type "application/x-www-form-urlencoded"

    Here is the top portion of my task with the PostAsync operation. I saw that the HttpStringContent has a mediatype that is a string but this setup isn't working. Do I have to format my Platform String differently or am I close? I am sending "grant_type=authorization_code" as my string. Any help is appreciated. Thank you!

    	create_task(
    		httpClient->PostAsync(resourceAddress, ref new HttpStringContent(mystring, UnicodeEncoding::Utf8, "application/x-www-form-urlencoded")),
    		cancellationTokenSource.get_token()).then([this](HttpResponseMessage^ response)
    	

    Thursday, March 13, 2014 10:47 PM

Answers

  • I figured it out, here is what my code looks like.

    	HttpStringContent^ http_data = ref new HttpStringContent(api_info, UnicodeEncoding::Utf8);
    	http_data->Headers->ContentType = ref new HttpMediaTypeHeaderValue("application/x-www-form-urlencoded");
    
    	create_task(
    		httpClient->PostAsync(resourceAddress, http_data),
    		cancellationTokenSource.get_token()).then([this](HttpResponseMessage^ response)
    	{

    • Marked as answer by DaveB9 Friday, March 14, 2014 3:13 PM
    Friday, March 14, 2014 3:13 PM

All replies

  • I figured it out, here is what my code looks like.

    	HttpStringContent^ http_data = ref new HttpStringContent(api_info, UnicodeEncoding::Utf8);
    	http_data->Headers->ContentType = ref new HttpMediaTypeHeaderValue("application/x-www-form-urlencoded");
    
    	create_task(
    		httpClient->PostAsync(resourceAddress, http_data),
    		cancellationTokenSource.get_token()).then([this](HttpResponseMessage^ response)
    	{

    • Marked as answer by DaveB9 Friday, March 14, 2014 3:13 PM
    Friday, March 14, 2014 3:13 PM
  • If you want to send in HTTP Form URL encode content, why not use the content class "HttpFormUrlEncodedContent"?  It's designed to do all the form url encoding correctly?

    Link is here

    And we have a poster that lists all of the content classes (column 3), along with mini-samples (sorry, they are C# and not C++) here


    Network Developer Experience Team (Microsoft)

    Saturday, March 15, 2014 8:41 PM