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.

5
  • 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. Commented Jan 4, 2023 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. Commented Jan 4, 2023 at 16:34
  • @MakePeaceGreatAgain My thought also the same as in this answer stackoverflow.com/a/20550558/954093 Commented Jan 4, 2023 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. Commented Jan 4, 2023 at 16:47
  • In this case I would prefer using OData which provides several query/ filter options example- Basic OData tutorial: link Commented Jan 4, 2023 at 17:57

2 Answers 2

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=....
Sign up to request clarification or add additional context in comments.

2 Comments

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..
@SandeepThomas Yes, when you use GET , you can only pass the input parameters as a query string
1

Try [FromQuery]:

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

2 Comments

Welcome to Stack Overflow! We try to avoid having answers that ask a question. I've reworded this to be a statement. When you have enough reputation, you'll be able to comment on others' posts. However, keep in mind that this question was posted over a year ago, so it's unlikely that the user is still searching for an answer and would be willing to dust off the code to try it ;-). We still do want new answers though, just that they'll be more useful to future readers.
Also, your answer could be improved with additional supporting information. If possible, please edit your post to add further details, such as explanations or documentation, so that others can confirm that your answer is correct. You can find more information in How do I write a good answer? - "Brevity is acceptable, but fuller explanations are better." It might be helpful to review some highly upvoted answers as examples to follow Thanks!

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.