Answered by:
Using HttpFormUrlEncodedContent

Question
-
I'm trying to use HttpFormUrlEncodedContent, in my HttpClient POST request but am unsure about the type of 'content' in the constructor.
The documentation say it needs to be type:
Type: IIterable<IKeyValuePair> [JavaScript/C++]
Which is the best datatype to use that implements this interface in JavaScript?
I've tried using propertyset -
var properties = new Windows.Foundation.Collections.PropertySet(); properties.insert('username', username); var data = new Windows.Web.Http.HttpFormUrlEncodedContent(properties);
But I get error, 'no interface supported'.
- Edited by BradStevenson Monday, December 16, 2013 11:36 AM added more information.
Saturday, December 14, 2013 8:29 PM
Answers
-
This one's a little funny (at least for JavaScript; the API is super simple in C#). The jist is: 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);And the super simple C# code?
var mymap = new Dictionary<string,string>() { {"name","Peter"}, {"office", "26/1667"}};
(warning: the C# code was typed by hand.)Network Developer Experience Team (Microsoft)
- Marked as answer by Matt SmallMicrosoft employee, Moderator Wednesday, December 18, 2013 7:54 PM
Tuesday, December 17, 2013 11:39 PM
All replies
-
Which line throws the exception?
Does it fail if you use an explicit new KeyValuepair?
properties.add (new KeyValuePair('username', username));
Matt Small - Microsoft Escalation Engineer - Forum Moderator
If my reply answers your question, please mark this post as answered.
NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.Monday, December 16, 2013 8:07 PMModerator -
Im doing this in JavaScript, KeyValuePair doesn't exist.
The error is thrown on line
var data = new Windows.Web.Http.HttpFormUrlEncodedContent(properties);
Tuesday, December 17, 2013 8:19 PM -
This one's a little funny (at least for JavaScript; the API is super simple in C#). The jist is: 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);And the super simple C# code?
var mymap = new Dictionary<string,string>() { {"name","Peter"}, {"office", "26/1667"}};
(warning: the C# code was typed by hand.)Network Developer Experience Team (Microsoft)
- Marked as answer by Matt SmallMicrosoft employee, Moderator Wednesday, December 18, 2013 7:54 PM
Tuesday, December 17, 2013 11:39 PM -
This has been working great for me however I have discovered an issue. How do I set a key named 'location' ?
e.g.
var keyValue = new Windows.Web.Http.HttpClient().defaultRequestHeaders; keyValue['location'] = 'home';
Throws the error:
0x80070057 - JavaScript runtime error: The parameter is incorrect.
Invalid HTTP headers.
WinRT information: Invalid HTTP headers.
How do I get around this?
Thanks
Wednesday, January 1, 2014 10:44 PM -
I think this is it:
http://msdn.microsoft.com/en-us/library/windows/apps/dn305252.aspx
var client= new Windows.Web.Http.HttpClient();
client.defaultRequestHeaders.Add("location","home");
Matt Small - Microsoft Escalation Engineer - Forum Moderator
If my reply answers your question, please mark this post as answered.
NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.Thursday, January 2, 2014 1:01 PMModerator