3

I am developing web api as an facade which will encapsulated request to underlying systems.

So, lets assume I have cars endpoint:

api/v1/cars

Now I want my api to get parameters which will determine calls to underlying systems.

Like:

{
   provider: 'service_1'.
   access_token: 'token_2',
   info: 'some_info'
},
{
   provider: 'service_2'.
   access_token: 'token_2',
   info: 'some_info'
}

Besides that api will take standard parameters like startdate, enddate, offset and others.

public async Task<Result<Cars>> Get([FromUri] RequestParams requestParams);

public class RequestParams
{
    public RequestParams()
    {
        Limit = 50;
        Offset = 0;
        StartDate = DateTime.Now;
        EndDate = DateTime.Now;
    }

    public string UserId { get; set; }
    public int Limit { get; set; }
    public int Offset { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
}

It's easy to map standard params from uri, but I do know how to properly pass json collection. Any ideas?

2 Answers 2

3

By definition, a GET request doesn't have payload (i.e. data in the body). So, the only way to pass data to a GET request is in the url, i.e. using route data or query string parameters.

If you need to pass a JSON object you need to use a different request method, usualy POST. This includes passing collections.

If you use POST, Web API will automatically load the parameter object with the JSON posted object. If the paramerter is a collection (for example a List<T>) it will also be correctly populated.

There is only one thing that you must take into account: the method can only have one parameter loaded from the body. I.e. you cannot receive several parameters in a Web API action from the body. (You can, however, have data coming from the URL and data coming from the body).

So, you need to change your request and your method to use POST or any other method with payload. Besides, you must choose one of thesse two options:

  1. create a class that includes all the standard parameters, and the collection, and use it as parameter, and post al lthe data in a single object, with the same structure.

  2. pass the standard parameters in the query string, or using route data, and the collection as JSON. In this case, yourmethod must have several parameters: onw for the collection posted as JSON, and one for each other parameters postes in the query string or route data

Posting a collection in the querystring, as proposed in kapsi's answer, is not possible, unless you make some kind of serialization of the parameter on the client side and deserialization when receiving it on the server side. That's overkill, just use POST or any other method with body, as explained above.

Sign up to request clarification or add additional context in comments.

Comments

0

If you for example use jQuery, you can use the ajax method to address this:

$.ajax({
    url: "./",
    type: "GET",
    data: {
        UserId: 1,
        Limit: 2,
        Offset: 2,
        StartDate: "02/15/2015",
        EndDate: "05/15/2015"
    }
});

jQuery takes action and following GET is made:

?UserId=1&Limit=2&Offset=2&StartDate=02%2F15%2F2015&EndDate=05%2F15%2F2015&_=1423137376902

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.