Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Aug 09, 2012 06:40 AM by agarwal.peeush
Member
12 Points
32 Posts
Aug 09, 2012 05:04 AM|LINK
Hi all,
I'm writing a demo Web API app having a class as
public class UserToken { public string UserName { get; set; } public string Token { get; set; } }
and calling a web api using this HttpClient code:
client = new HttpClient(); client.BaseAddress = new Uri("http://xyz.com/"); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); HttpResponseMessage response = client.GetAsync("login/" + username + "/" + password).Result; if (response.IsSuccessStatusCode) { var userToken = response.Content.ReadAsAsync<UserToken>().Result; token = userToken.Token; }
While debugging, i find that response is coming, but I'm getting exception while it is reading from content as
No MediaTypeFormatter is available to read an object of type 'UserToken' from content with media type 'text/html'
My query here is that when I'm adding Accept as "application/json" in Request-header, then why I'm getting response content as "text.html"?
Kindly suggest if my understanding is wrong here.
All-Star
28084 Points
4997 Posts
MVP
Aug 09, 2012 06:08 AM|LINK
Debug the network call with fiddler and make sure you're getting back the response you expect. My guess is that you're calling the wrong controller (perhaps a MVC controller and not a WebAPI controller thus the HTML?).
Aug 09, 2012 06:40 AM|LINK
Hi Brock,
Its done! I was calling a wrong http link... Sorry my bad.
But then aslo I was missing Proxy settings in the client configuration, which I resolved with adding HttpClientHandler ans setting proxy as
var handler = new HttpClientHandler(){ Proxy = HttpWebRequest.GetSystemWebProxy() }; client = new HttpClient(handler);
Thanks for response!
agarwal.peeu...
Member
12 Points
32 Posts
No MediaTypeFormatter is available to read an object of type 'UserToken' from content with media ...
Aug 09, 2012 05:04 AM|LINK
Hi all,
I'm writing a demo Web API app having a class as
public class UserToken { public string UserName { get; set; } public string Token { get; set; } }and calling a web api using this HttpClient code:
client = new HttpClient(); client.BaseAddress = new Uri("http://xyz.com/"); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); HttpResponseMessage response = client.GetAsync("login/" + username + "/" + password).Result; if (response.IsSuccessStatusCode) { var userToken = response.Content.ReadAsAsync<UserToken>().Result; token = userToken.Token; }While debugging, i find that response is coming, but I'm getting exception while it is reading from content as
No MediaTypeFormatter is available to read an object of type 'UserToken' from content with media type 'text/html'
My query here is that when I'm adding Accept as "application/json" in Request-header, then why I'm getting response content as "text.html"?
Kindly suggest if my understanding is wrong here.
BrockAllen
All-Star
28084 Points
4997 Posts
MVP
Re: No MediaTypeFormatter is available to read an object of type 'UserToken' from content with me...
Aug 09, 2012 06:08 AM|LINK
Debug the network call with fiddler and make sure you're getting back the response you expect. My guess is that you're calling the wrong controller (perhaps a MVC controller and not a WebAPI controller thus the HTML?).
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
agarwal.peeu...
Member
12 Points
32 Posts
Re: No MediaTypeFormatter is available to read an object of type 'UserToken' from content with me...
Aug 09, 2012 06:40 AM|LINK
Hi Brock,
Its done! I was calling a wrong http link... Sorry my bad.
But then aslo I was missing Proxy settings in the client configuration, which I resolved with adding HttpClientHandler ans setting proxy as
var handler = new HttpClientHandler(){ Proxy = HttpWebRequest.GetSystemWebProxy() }; client = new HttpClient(handler);Thanks for response!