I am trying to consume a WebAPI from a console application, as part of an integration test. I use this method:
private static string Consume(string endpoint, string user, string password)
{
var client = new HttpClient();
client.BaseAddress = new Uri(endpoint);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
string body = "{\"User\":\"" + user + "\",\"Password\":\"" + password + "\"}";
var response = client.PostAsync("api/ticket", new StringContent(body, Encoding.UTF8, "application/json")).Result;
response.EnsureSuccessStatusCode();
if (response.IsSuccessStatusCode)
... //process
return string.Empty;
}
I don't feel very satisfied with the way I am sending the parameters to the WebAPI method, I prefer to use a typed object like:
public class Credentials
{
public string User { get; set; }
public string Password { get; set; }
}
and put it in the body. Does anyone has a suggestion for this?