Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass multiple parameters in a POST API without using a DTO class in .Net Core MVC

I have an action on my web project which calls to an API

    [HttpPost]
    public async Task<IActionResult> ExpireSurvey(int id)
    {
        var token = await HttpContext.GetTokenAsync("access_token");

        using (var client = new HttpClient())
        {
            client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);

            var path = "/api/forms/ExpireSurvey";
            var url = Domain + path;
            var data = JsonConvert.SerializeObject(id);
            HttpContent httpContent = new StringContent(data, Encoding.UTF8, "application/json");
            var response = await client.PutAsync(url, httpContent);

            return Json(response);
        }
    }

In the API project this is received as follows:

[HttpPut]
    public IActionResult ExpireSurvey([FromBody] int surveyId)
    {
        _repository.ExpireSurvey(surveyId, expiryDate);
        return Ok();
    }

This works fine - however, say I want to pass in an int id and a DateTime variable, how do I serialise and pass them both into the HttpContent? I can do it with a DTO object, but I don't want to be setting up DTO objects when there is only two fields.

like image 693
egmfrs Avatar asked Mar 29 '18 14:03

egmfrs


4 Answers

You can pass multiple parameters in as URL as below example

Parameter name must be the same (case-insensitive), If names do not match then values of the parameters will not be set.

[HttpPost]
[Route("{surveyId}/{expiryDate}")]
public IActionResult Post(int surveyId, DateTime expiryDate)
{
    return Ok(new { surveyId, expiryDate });
}

Call URL

http://localhost:[port]/api/[controller]/1/3-29-2018
like image 139
ElasticCode Avatar answered Nov 04 '22 23:11

ElasticCode


Based on the answers above, I got the following code working. Hope this helps someone! (thanks to others of course for getting me on the right track)

/// <summary>
/// Post api/dostuff/{id}
[HttpPost]
[Route("dostuff/{id}")]
public async Task<IActionResult> DoStuff([FromBody]Model model, int id)
{
    // Both model and id are available for use!
}
like image 24
Ross Avatar answered Oct 06 '22 00:10

Ross


You can use anonymous types like this

var x = new { id = 2, date = DateTime.Now };
var data = JsonConvert.SerializeObject(x);

When receiving the data, you can only have one [FromBody] parameter. So that doesn't work for receiving multiple parameters (unless you can put all but one into the URL). If you don't want to declare a DTO, you can use a dynamic object like this:

[HttpPost]
public void Post([FromBody] dynamic data)
{
    Console.WriteLine(data.id);
    Console.WriteLine(data.date);
}

Don't overdo using anonymous types and dynamic variables though. They're very convenient for working with JSON, but you lose all type checking which is one of the things that makes C# really nice to work with.

like image 12
Hans Kilian Avatar answered Nov 04 '22 23:11

Hans Kilian


I think it would be helpful to recognize that ASP.NET Core is REST-based and REST fundamentally deals with the concept of resources. While not an unbreakable rule, the general idea is that you should have what you're calling DTOs here. In other words, you're not posting distinct and unrelated bits of data, but an object that represents something.

This becomes increasingly important if you start mixing in things like Swagger to generate documentation for your API. The objects you create become part of that documentation, giving consumers of your API a template for follow in the development of their apps.

Long and short, I'd say embrace the concept of resources/objects/DTOs/whatever. Model the data your API works with. It will help both you as a developer of the API and any consumers of your API.

like image 7
Chris Pratt Avatar answered Nov 04 '22 23:11

Chris Pratt