I have generic method to communicate with my Swagger API:
get<ReturnType>(url: string, params?: HttpParams): Observable<ReturnType> {
return this.http.get<ReturnType>(environment.apiUrl + url, {
params: params,
});
}
But when I make request with params it tries to reach this endpoint http://localhost:3000/user/get?id=10
instead of http://localhost:3000/user/get/10
On the backend side there is:
@Get('get/:id')
@ApiResponse({ type: UserDto, status: 201 })
getUser(@Param('id') id: number): UserDto {
return { id: id, name: 'John Doe', email: 'john.doe@example.com' };
}
Where is mistake and how can I fix it?
You're looking to use query parameters over url parameters. Remove the :id
from the @Get()
and use @Query('id')
instead of @Param('id')
. Everything else should be fine once you update the get request