M
M
mitaichik2018-04-11 03:04:22
Angular
mitaichik, 2018-04-11 03:04:22

HttpClient: how to bind parameters to url?

Hello. I'm familiar with Angular.
I need to make a request to the address http://test/api/v1/person/10
It is clear that 10 is the id of the object, and it changes.
So, how to substitute this id in the url? I understand that you can stupidly add it to the url, but, IMHO, this is somehow not right.
I expected to see something like:

getPerson(id: number) : Observable<Person> {
  return this.http
    .get<Person>("http://test/api/v1/person/{id}", {pathParams : {id : id.toString()}});
}

Where {id}is the placeholder for the parameter, but pathParamsthe parameters themselves are passed to. But I didn't find it in the documentation. Isn't there something like this out of the box?
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sasha Novik, 2018-04-23
@mitaichik

That very simple option is the answer:

getPerson(id: number): Observable<Person> {
  return this.http.get<Person>(`http://test/api/v1/person/${id}`);
}

It's just a url string and the question is, why complicate it and what additional tasks need to be solved?

D
Dmitry Eremin, 2018-04-11
@EreminD

dock
example
and in general

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question