2

I have a WebApi service that calculates a price of a customized product. The controller function is:

public double Get([FromUri]Specifications specifications)

Specifications is a class that allows to customize the product:

public class Specifications 
{
    public string Currency;
    public int DesktopLicenses;
    public Product Product;
    public int Licenses;
}

Now, how can I consume this service from C#. I want to avoid to codify manually the URI query with all Specifications variables, I would like to able to use directly an instance of Specificationsto call the service.

If the service is a POST, I could do it doing:

Specifications product = new Specifications( ...);
HttpResponseMessage reponse = httpClient.PostAsJsonAsync("api/pricecalculator", product).Result;

but I cannot find the way to do the same when I use GET.

4
  • This is close to what you're asking for - it almost looks like you have to do a string builder. Which sort of defeats the idea of passing an object. POST is certainly the best way to handle it -- is there no way you can do it via POST? It removes the headache of complext object transports. stackoverflow.com/questions/13585992/… Commented Jul 10, 2014 at 14:03
  • Yes, I sow this question, but precisely I want to avoid to codify manually all members of the class in the URI. Yes, I could use POST, but according the guidelines, POST must be used when you save some data in the server and GET when you simply get data. Commented Jul 10, 2014 at 14:07
  • comment area was too long, see my "answer" below. In short, if you need to feed the server some complex data to get a result, you should be using POST. To feed it via the querystring is only going to infuse more headache than necessary. Commented Jul 10, 2014 at 14:12
  • Possible duplicate of Web API Get Method with Complex Object as Parameter Commented Apr 24, 2017 at 15:40

1 Answer 1

3

The example is showing that the GET is passing it a complex object in the call. Normally, that's just a simple request, and returning the complex object -- that's the "best practice". If you need to request something by giving it a complex object - it should still be a POST call. I know the pundits like to think POST/PUT as your change/add for the REST world -- but in the end, frankly there's zero difference between a POST and a GET besides the request body. If you need to give the server complex data, use the request body (aka POST). If it's a simple request -- /api/listofvendors/zone1 - then use a GET.

Web API Get Method with Complex Object as Parameter

example:

    [HttpGet]
    [Route("~/services/mrf/{mrfnumber}")]                       // GET specific MRF
    public Mrf GetMrfRecord(string mrfnumber) {
        using (var ddc = new MRFDataContext(ConnectionString)) {
            var options = new DataLoadOptions();
            options.LoadWith((Mrf c) => c.MRFParts);    //immediate load related MRFParts
            ddc.LoadOptions = options;

            var mrf = (from u in ddc.Mrfs
                       where u.MrfNum == mrfnumber
                       select u).FirstOrDefault();

            return mrf ?? null;
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.