Reputation: 22411
How can we call the following web api ?
[HttpPost]
public bool ValidateAdmin(string username, string password)
{
return _userBusinessObject.ValidateAdmin(username, password);
}
I've written the following code, but it dosen't work 404 (Not Found)
string url = string.Format("api/User/ValidateAdmin?password={0}", password);
HttpResponseMessage response = Client.PostAsJsonAsync(url, username).Result;
response.EnsureSuccessStatusCode();
return response.Content.ReadAsAsync<bool>().Result;
Edit:
I'm dead sure about the Url, but it says 404 (Not Found)
Upvotes: 1
Views: 4986
Reputation: 2378
i do like this for me in similar case :
MediaTypeFormatter jsonFormatter = new JsonMediaTypeFormatter();
HttpContent datas = new ObjectContent<dynamic>(new {
username= username,
password= password}, jsonFormatter);
var client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
var response = client.PostAsync("api/User/ValidateAdmin", datas).Result;
if (response != null)
{
try
{
response.EnsureSuccessStatusCode();
return response.Content.ReadAsAsync<bool>().Result;
...
Upvotes: 2