3

I have to retrieve orders from my API made in NET Core.

The retrieval is made in an action of my MVC controller calling an endpoint of another NET Core APP using GET.

The API endpoint is the following one:

API:

[HttpGet("orders/orderSearchParameters")]
    public IActionResult Get(OrderSearchParameters orderSearchParameters)
    {
        var orders = MenuService.GetMenuOrders(new GetMenuOrdersRequest { From = orderSearchParameters.From, To = orderSearchParameters.To, FoodProviderId = orderSearchParameters.FoodProviderId }).Orders;
        return Ok(orders);
    }

An action of my Web App MVC controller must call that endpoint, and for that this is the following code:

public IActionResult GetOrders(OrderSearchParametersModel orderSearchParameters)
    {
        var uri = string.Format(ApiUri + "menus/orders/");

        using (HttpClient httpClient = new HttpClient())
        {
            var response = httpClient.GetStringAsync(uri);
            if (response.IsCompleted)
            {
                var orders = JsonConvert.DeserializeObject<List<OrderModel>>(response.Result);
                return Ok(orders);
            }
            else
            {
                return BadRequest();
            }
        }            
    }

What I can´t find is how can I serialize OrderSearchParametersModel to perform the GET operation with the HttpClient in the MVC controller.

In the attached code I do the GET without the incoming object.

How can I send this object using GET operation with HttpClient?

3
  • 2
    If you put all your parameters in the querystring, they will be translated into an OrderSearchParametersModel but they need to match this model properties names. Commented Mar 30, 2017 at 18:14
  • 1
    With an http GET you can't send an object. all you can send are query parameters and header strings. Typically the query info is sent via the query strign as @MichelAmorosa mentioned. Commented Mar 30, 2017 at 21:08
  • 1
    Thanks @MichelAmorosa. I you want to put that in an aswer I can choose it Commented Apr 4, 2017 at 14:36

1 Answer 1

4

If you put all your parameters in the querystring, they will be translated into an OrderSearchParametersModel but they need to match this model properties names.

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.