0

I am developing a dashboard in react which calls backend API to fetch all recipes from the database. So the search criteria would be huge. Its required to pass many filter attributes into the backend to get the correct recipes.

As an example below I have defined a class for Search Parameters

public class SearchParams
{
    public string TemplateName { get; set; } = "";
    public DateTime DateFrom { get; set; }
    public DateTime DateTo { get; set; }
    public String CreatedBy { get; set; } = "";
    public Guid Id { get; set; }

}

So the GET method is required to handle whatever the parameters provided fetch the corresponding recipes from the DB accordingly.

But since GET requests doesnt support accepting parameters as OBJECT (SOrry if I am wrong) I thought about trying with POST. But that feels a little confused to use POST for a search functionality.

So with GET method do I need to define with all the parameters like this

[HttpGet]
public IEnumerable<Recipes> Get(string TemplateName,DateTime DateFrom....)
{
    return new string[] { "value1", "value2" };
}

Or any best approach for this?

Please note, my real search criteria include many attributes other than the properties in my class definition above.

Sandeep Thomas
  • 4,303
  • 14
  • 61
  • 132
  • 2
    it's completely valid to use POST for search-operations when the data to be transferred gets too huge or too complex. GET and POST aren't about readonly- or write-access. – MakePeaceGreatAgain Jan 04 '23 at 16:32
  • If you're looking for the simplest answer, it would probably be to just read your parameters from the querystring. The pro is that you don't need to define any of them in the Get(), the downside is that they would not be required so you'd need to make sure the query parameter is actually defined. – Erica Stockwell-Alpert Jan 04 '23 at 16:34
  • @MakePeaceGreatAgain My thought also the same as in this answer https://stackoverflow.com/a/20550558/954093 – Sandeep Thomas Jan 04 '23 at 16:44
  • that's why I said POST isn't neccessarily about read- or write-access, but just about if you need a request-body or if you transport the entire data in URL-params. – MakePeaceGreatAgain Jan 04 '23 at 16:47
  • In this case I would prefer using [OData](https://learn.microsoft.com/en-us/aspnet/web-api/overview/odata-support-in-aspnet-web-api/odata-v4/create-an-odata-v4-endpoint) which provides several query/ filter options [example](https://devblogs.microsoft.com/odata/up-running-w-odata-in-asp-net-6/#filtering)- Basic OData tutorial: [link](https://www.odata.org/getting-started/basic-tutorial/) – Odrai Jan 04 '23 at 17:57

1 Answers1

1

nothing prevents you from using SearchParams as an input parameters

[HttpGet]
public IEnumerable<Recipes> Search(SearchParams par)

the only problem is that Get doesn't include a body, so all data should be included in a query string

.../search?TemplateName=TemplateName&CreatedBy=....
Serge
  • 40,935
  • 4
  • 18
  • 45
  • Perfect.. this is something I didnt tried... So even if we specify it as an object parameter we can pass it values as query string, isnt it? Sorry if I interpret it wrong.. – Sandeep Thomas Jan 05 '23 at 06:34
  • @SandeepThomas Yes, when you use GET , you can only pass the input parameters as a query string – Serge Jan 05 '23 at 14:33