pete
pete

Reputation: 365

Angular 8: Why do I receive null when using URL Query Strings?

I am trying to consume the following web-service

@POST
@Path("/delete")
@Produces(MediaType.APPLICATION_JSON)
public boolean deletePost(@QueryParam("idPost") String idPost) {
// logic here
}

However, trying to assign a value to the QueryParam idPost from angular does not work. Always idPost arrives with a value of null.

Try the 2 forms specified in the documentation but it does not work for me. What am I doing wrong?

public deletePost(idPost: number): Observable<any> {
    const options = idPost ?
    { params: new HttpParams().set('idPost', idPost.toString()) } : {};
    return this.httpClient.post('url-delete',options);
}

public deletePost(idPost: number): Observable<any> {
    const params = new HttpParams({fromString: 'idPost=' + idPost.toString()});
    return this.httpClient.post('url-delete',params);
}

Upvotes: 0

Views: 76

Answers (1)

griFlo
griFlo

Reputation: 2164

The 2nd Paramter in post is the body and the options should be a json like:

const options = { params: new HttpParams().set('name', 'xxx') }

Now invoke it like this:

public deletePost(idPost: number): Observable<any> {
    const options = {params: new HttpParams({fromString: 'idPost=' + idPost.toString()})};
    return this.httpClient.post('url-delete',{}, options); // empty body
}

Upvotes: 2

Related Questions