Our backend developer implemented an endpoint that allows GET method to get a list of Assets of the database. To filter the Assets that I want in the response, I need to pass some parameters in the URL.
First I implemented the following function to mount the GET request with all available parameters:
fetchAssetList(data) {
let request_url = 'asset-mgt/assets?'
if (data.name !== '' && data.name !== null){
request_url = request_url + "Name=" + data.name + "&"
}
if (data.assetCode !== '' && data.assetCode !== null){
request_url = request_url + "AssetCode=" + data.assetCode + "&"
}
if (data.parentAssetName !== '' && data.parentAssetName !== null){
request_url = request_url + "ParentAssetName=" + data.parentAssetName + "&"
}
if (data.type !== -1 && data.type !== null && data.type !== undefined){
request_url = request_url + "Type=" + data.type + "&"
}
if (data.parentAssetId !== -1 && data.parentAssetId !== null && data.parentAssetId !== undefined){
request_url = request_url + "ParentAssetId=" + data.parentAssetId + "&"
}
return axiosPrivate.get(request_url);
},
I am not really happy with the current solution we implemented. The GET request with such many query parameters does not feel right.
Do you think it would be better to have one GET Method which always returns all assets (.../assets), other GET method which takes the id of the asset (.../assets/{id}) and one POST method, which takes a payload to filter (.../assets/filter). What is the best practice?