Home > Software engineering >  Angular how to build the requestBody in http calls?
Angular how to build the requestBody in http calls?

Time:10-25

Imagine i have multiple input fields:

projectName, customerName, employeeName

The user can fetch data from an API depending on which values he input on this fields. If he enters some values, the RequestBody will maybe look like this:

{
    "projectName": "Opel Travel",
    "customerName": "",
    "employeeName": "",
}

How should i build the requestBody when the user types nothing into the fields, to get all data without filtering for values?

variant1:

{
    "projectName": "",
    "customerName": "",
    "employeeName": "",
}

The API dont return any data, so i guess this approach is wrong.

variant2:

{

}

with this approach i get all data.

What is best practice to build the request body? The Goal is, to get all data if user inputs nothing otherwise filter for his inputs.

CodePudding user response:

Well, it also depends upon how you handled null cases while inserting the data. And filtering through parameters has nothing much to do on the front-end side. If any parameter (input field) is "" in the request, you should manage to convert the empty string to null or 0 depending on how you sent the data while inserting into the databases.

Let suppose you sent this object to save in the databases

{
    "projectName": "Opel Travel",
    "customerName": "",
    "employeeName": "",
}

"customerName" and "employeeName" should be saved as null as they contain no value.

  • Related