What's the recommended way to pass path params in Angular 2 http object

Viewed 6737

I am using angular 2, I need to do a delete request to a backend having a path param like this

import { Http } from "@angular/http";

deletePlayer(id: string): Observable<any> {
        return this.http.delete("/api/players/{id}");
}

My question is, what is the best way to pass the id path param to the http object. I've used UrlSearchParams for the query parameters but this does not seem to have an option for path params. The docu is not clear about this either.

2 Answers

You should always escape any string, that might contain non-static values (user inputs, database values, etc.).

import { Http } from "@angular/http";

deletePlayer(id: string) {
    return this.http.delete('/api/players/' + encodeURIComponent(id));
}

encodeURIComponent will keep you safe.

Related