I am trying to consume the below web api via console app using Httpclient. I am stuck as in how to pass the parameter. The paramter is coming as a command line argument.
This is my Rest api
[HttpPost, Route("Test")]
public IHttpActionResult Test(bool sample = false)
{
return Ok();
}
The parameter comes in this was as command line argument
/Parameters:sample=true.
Here is how I am parsing out the parameter in the console app
static int Main(string[] args)
{
if (args.Length == 0)
{
Console.Error.WriteLine("No action provided.");
return -1;
}
foreach (string param in args)
{
switch (param.Substring(0, param.IndexOf(":")))
{
case "/Action":
action = param.Substring(param.IndexOf(":") + 1);
break;
case "/Parameters":
parameter = param.Substring(param.IndexOf(":") + 1);
break;
}
}
return 0;
}
Once I get my parameter which is in this format
parameter = "sample=true"
I am trying to invoke the web api call but unable to pass the parameter value. can anybody pin point what I am doing wrong
client.BaseAddress = new Uri(ConfigurationManager.AppSettings.Get("BaseWebApiUrl"));
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var apiUrl = GetApiURL();
var Context = new StringContent(JsonConvert.SerializeObject(parameter), Encoding.UTF8, "application/json");
var respFeed = await client.PostAsync(apiUrl, Context);
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…