HttpFormUrlEncodedContent object but without encoding the paremeters RRS feed

  • Question

  • I'm using the HttpFormUrlEncodedContent object in my application for a PostAsync method of Windows.Web.Http.HttpClient, and I have it set like so:

    HttpFormUrlEncodedContent httpFormUrlEncodedContent = new HttpFormUrlEncodedContent(new[] {
        new KeyValuePair<string, string>("parameterOne", "value of one"),
        new KeyValuePair<string, string>("parameterTwo", "value of two"),
        new KeyValuePair<string, string>("parameterThree", "value of three"),
        new KeyValuePair<string, string>("scope", "FIRST_PARAMETER+SECOND_PARAMETER+THIRD_PARAMETER")
    HttpClient httpClient = new HttpClient();
    response = await httpClient.PostAsync("", httpFormUrlEncodedContent);

    My problem with this is that the scope is encoded, and when it gets to the server throws an error, because encoded looks like this:


    So, is there some kind of HttpFormUrlUnencodedContent or some other way of defining the content of my PostAsync method?

    &quot;Digam-me os vossos nomes e eu mato-vos por ordem alfabética!&quot;

    Friday, October 31, 2014 9:51 AM

All replies

  • the API technically works perfectly, but it requires an IIterable<IKeyValuePair>.  But there's no obvious datatype in JavaScript that you can use for one of those.  As you found out, a plain old object (which ideally would "just work") doesn't.

    But there's a clever workaround!  All you have to do is find some object in the Windows Runtime that also supported that special interface.  And it turns out that there's one good candicate: a RequestHeaders object (!).

    Here's some working code:

    var keyValue = (new Windows.Web.Http.HttpClient()).defaultRequestHeaders;
    keyValue["name"] = "Peter";
    keyValue["office"] = "26/1167";
    var c = new Windows.Web.Http.HttpFormUrlEncodedContent(keyValue);

    Monday, November 3, 2014 10:30 AM
  • Thank you Qndssdg UIDjsg for your answer, however it does not work. I'm guessing it's because when the HttpFormUrlEncodedContent is created, everything in the keyValue is Encoded, because in fact is.

    &quot;Digam-me os vossos nomes e eu mato-vos por ordem alfabética!&quot;

    Monday, November 3, 2014 11:10 AM