Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
614 views
in Technique[技术] by (71.8m points)

.net - Httpclient consume web api via console app C#

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

By default in Web Api basic type parameters are never bound to the body of the request.

If you want to forcefully bind your bool parameter with the request body you need to decorate it with FromBodyAttribute:

[HttpPost, Route("Test")]
public IHttpActionResult Test([FromBody] bool sample = false)
{             
    return Ok();
}

Be aware that even if you do this your request is not valid for Web Api. A single basic type parameter must be passed with a specific format. In your case your request body must be the following:

=true

A better approach is to turn your Action parameter into a class:

public class TestModel
{
    public bool Sample { get; set; }
}

[HttpPost, Route("Test")]
public IHttpActionResult Test(TestModel model)
{
    return Ok();
}

This way you will be able to send a Json object as request body, like:

{
    "sample": true
}

This is a small example of how you could achieve such a result:

var parameterDictionary = parameter.Split("=").ToDictionary(s => s[0], s => bool.Parse(s[1]));
var json = JsonConvert.SerializeObject(parameterDictionary);
var content = new StringContent(json, Encoding.UTF8, "application/json");
var respFeed = await client.PostAsync(apiUrl, content);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...