I have a Project that acts as my client for the service. So multiple front ends can reference the same client project.
The client project will take data from MVC, WinForms, WPF, whatever, and post the data for you. So the client project takes a POCO, and posts it to the Web API.
For now, I'm not posting data from a web page directly to the service. That is why I'm using PostAsync and PutAsync. I'm using those to send data to the Web API service. I really don't care how the data is serialized, I was just trying to use a JSON one
in this case.
Is my problem more clear now? Should I be using HttpClient to post/put data like this? Or is there something easier I can/should use, at least until the Web API is more baked?
Ah ok that explains it :) I thought you had a "front end" Web API that was re-posting data to a backend one (I assumed for some security reasons). Now the generic code makes more sense ;)
And yes you should absolutely use HttpClient to interect with Web APIs, it's a great client.
If you debug your client code, where is it hanging? Is the URL in your _endpoint variable correct?
There are a few ways to encode a POCO object into JSON for PUT/POST, and afaik your method of using the HttpRequestMessage should work.
You can also use StringContent or StreamContent, and a Json serializer to encode your class.
Yes I did miss that, twice apparently! Thank you for pointing it out to me again. I'm going to try reverting to the default Formatter and see if that works.
skippyfire
Member
34 Points
31 Posts
Re: How to correctly use PostAsync and PutAsync?
Feb 23, 2012 04:41 PM|LINK
Sorry, I don't think I explained it well.
I have a Project that acts as my client for the service. So multiple front ends can reference the same client project.
The client project will take data from MVC, WinForms, WPF, whatever, and post the data for you. So the client project takes a POCO, and posts it to the Web API.
For now, I'm not posting data from a web page directly to the service. That is why I'm using PostAsync and PutAsync. I'm using those to send data to the Web API service. I really don't care how the data is serialized, I was just trying to use a JSON one in this case.
Is my problem more clear now? Should I be using HttpClient to post/put data like this? Or is there something easier I can/should use, at least until the Web API is more baked?
SiggiGG
Member
265 Points
105 Posts
Re: How to correctly use PostAsync and PutAsync?
Feb 23, 2012 05:02 PM|LINK
Ah ok that explains it :) I thought you had a "front end" Web API that was re-posting data to a backend one (I assumed for some security reasons). Now the generic code makes more sense ;)
And yes you should absolutely use HttpClient to interect with Web APIs, it's a great client.
If you debug your client code, where is it hanging? Is the URL in your _endpoint variable correct?
There are a few ways to encode a POCO object into JSON for PUT/POST, and afaik your method of using the HttpRequestMessage should work.
You can also use StringContent or StreamContent, and a Json serializer to encode your class.
skippyfire
Member
34 Points
31 Posts
Re: How to correctly use PostAsync and PutAsync?
Feb 23, 2012 05:33 PM|LINK
It hangs when it is trying to get the result from the request.
My endpoint is valid for GETs and DELETEs.
Not sure what else it could be. Has anyone posted any sample for making POST and PUT requests FROM the Web API?
Kiran Challa
Participant
1460 Points
285 Posts
Microsoft
Re: How to correctly use PostAsync and PutAsync?
Feb 23, 2012 05:35 PM|LINK
I think you missed my post in this discussion thread: http://forums.asp.net/post/4847638.aspx
Kiran Challa
skippyfire
Member
34 Points
31 Posts
Re: How to correctly use PostAsync and PutAsync?
Feb 23, 2012 05:36 PM|LINK
FYI, I did try this from a simple HTML page with some jQuery, and I was able to make a PUT request. So the problem is definitely client side for me.
SiggiGG
Member
265 Points
105 Posts
Re: How to correctly use PostAsync and PutAsync?
Feb 23, 2012 05:51 PM|LINK
Interesting. Please try using a StringContent or StreamContent class instead of the HttpRequestMessage you are doing.
Kiran Challa
Participant
1460 Points
285 Posts
Microsoft
Re: How to correctly use PostAsync and PutAsync?
Feb 23, 2012 06:30 PM|LINK
SkippyFire, I have posted the reason behind the issue here: http://forums.asp.net/post/4847638.aspx
Kiran Challa
skippyfire
Member
34 Points
31 Posts
Re: How to correctly use PostAsync and PutAsync?
Feb 23, 2012 06:44 PM|LINK
Hi Kiran,
Yes I did miss that, twice apparently! Thank you for pointing it out to me again. I'm going to try reverting to the default Formatter and see if that works.
Thanks again.
logic_rabbit
Member
15 Points
8 Posts
Re: How to correctly use PostAsync and PutAsync?
Feb 27, 2012 08:01 AM|LINK
I have simillar issues :
I have code like this on serverSide :
public HttpResponseMessage<Contact> Post(HttpRequestMessage<Contact> request) { var pero = request.Content.ReadAsAsync(typeof(Contact)).Result;Problem is that pero variable is always null
maggie.ying
Member
183 Points
39 Posts
Microsoft
Re: How to correctly use PostAsync and PutAsync?
Feb 27, 2012 08:37 AM|LINK
logic_rabbit, you can acutally get the request from the ApiController instance:
public HttpResponseMessage<Contact> Post() { var pero = this.Request.Content.ReadAsAsync(typeof(Contact)).Result;