Answered by:
How do I use the Javascript HttpClient.PutAsync and PostAsync methods

Question
-
Here is the documentation for the HttpClient.PutAsync method
http://msdn.microsoft.com/en-us/library/windows/apps/windows.web.http.httpclient.putasync?cs-save-lang=1&cs-lang=javascript#code-snippet-1
where it says the second parameter content should be of type IHttpContent (an interface), which doesn't exist in Javascript.
So, what should be the type for content when using the WinJS library. I've tried using
Windows.Web.Http.HttpStreamContent(fileStream);
but that just gives me the error "The parameter is incorrect".
Saturday, October 26, 2013 12:12 AM
Answers
-
Have you looked at the HttpClient sample of Windows 8.1? http://code.msdn.microsoft.com/HttpClient-sample-55700664
S4-post-text.js has the exact line that you are referring to.
var stringContent = new Windows.Web.Http.HttpStringContent(document.getElementById("requestBodyField").value); httpPromise = httpClient.postAsync(resourceAddress, stringContent).then(function (response) { return Helpers.displayTextResultAsync(response, outputField); });
Windows Store Developer Solutions, follow us on Twitter: @WSDevSol|| Want more solutions? See our blog
- Marked as answer by Prashant H PhadkeMicrosoft employee, Moderator Monday, October 28, 2013 11:54 PM
Saturday, October 26, 2013 12:44 AMModerator -
When you see an interface in JS, it means you need an instance of an object that has the IHttpContent members. In this case you can either use an HttpStringContent or HttpStreamContent object. It certainly could be documented more clearly. Uses of both of these are in the sample that Prashant pointed to.
Kraig
Author, Programming Windows 8 Apps with HTML, CSS, and JavaScript, a free ebook from Microsoft Press
Also see second edition preview
- Marked as answer by Prashant H PhadkeMicrosoft employee, Moderator Monday, October 28, 2013 11:54 PM
Saturday, October 26, 2013 3:15 AM -
In the HttpClient poster, these are all listed in the "IHttpContent" section. Out of the box, there are 6 classes that implement the IHttpContent interface: HttpBufferContent, HttpFormUrlEncodedContent, HttpMultipartContent (and the similar HttpMultipartFormDataContent), HttpStreamContent, and HttpStringContent.
In general, you should pick the class that matches your data: if you have a string, use HttpString content, etc.
The Windows.Web.Http HttpClient poster is at http://www.microsoft.com/en-us/download/details.aspx?id=40018
Network Developer Experience Team (Microsoft)
- Marked as answer by Prashant H PhadkeMicrosoft employee, Moderator Monday, October 28, 2013 11:54 PM
Monday, October 28, 2013 5:50 PM
All replies
-
Have you looked at the HttpClient sample of Windows 8.1? http://code.msdn.microsoft.com/HttpClient-sample-55700664
S4-post-text.js has the exact line that you are referring to.
var stringContent = new Windows.Web.Http.HttpStringContent(document.getElementById("requestBodyField").value); httpPromise = httpClient.postAsync(resourceAddress, stringContent).then(function (response) { return Helpers.displayTextResultAsync(response, outputField); });
Windows Store Developer Solutions, follow us on Twitter: @WSDevSol|| Want more solutions? See our blog
- Marked as answer by Prashant H PhadkeMicrosoft employee, Moderator Monday, October 28, 2013 11:54 PM
Saturday, October 26, 2013 12:44 AMModerator -
When you see an interface in JS, it means you need an instance of an object that has the IHttpContent members. In this case you can either use an HttpStringContent or HttpStreamContent object. It certainly could be documented more clearly. Uses of both of these are in the sample that Prashant pointed to.
Kraig
Author, Programming Windows 8 Apps with HTML, CSS, and JavaScript, a free ebook from Microsoft Press
Also see second edition preview
- Marked as answer by Prashant H PhadkeMicrosoft employee, Moderator Monday, October 28, 2013 11:54 PM
Saturday, October 26, 2013 3:15 AM -
In the HttpClient poster, these are all listed in the "IHttpContent" section. Out of the box, there are 6 classes that implement the IHttpContent interface: HttpBufferContent, HttpFormUrlEncodedContent, HttpMultipartContent (and the similar HttpMultipartFormDataContent), HttpStreamContent, and HttpStringContent.
In general, you should pick the class that matches your data: if you have a string, use HttpString content, etc.
The Windows.Web.Http HttpClient poster is at http://www.microsoft.com/en-us/download/details.aspx?id=40018
Network Developer Experience Team (Microsoft)
- Marked as answer by Prashant H PhadkeMicrosoft employee, Moderator Monday, October 28, 2013 11:54 PM
Monday, October 28, 2013 5:50 PM