0

How to send delete request to REST api using angular?

I want send delete with id: 1

I try:

this.http.delete(environment.apiUrl+"id="+1).subscribe(data => {

});

Where http is private http: HttpClient, but it not working.

Value of environment.apiUrl is http://localhost/deleteendpoint

3
  • That URL looks strange. What is the value of environment.apiUrl? Commented Dec 10, 2017 at 21:15
  • Value of environment.apiUrl is localhost/deleteendpoint Commented Dec 10, 2017 at 21:16
  • 2
    So maybe a ? is missing. Try with this.http.delete(environment.apiUrl+"?id="+1).subscribe(data=>{}); Commented Dec 10, 2017 at 21:18

2 Answers 2

2
const params = new HttpParams().set('id', '1');

this.http.delete(environment.apiUrl, { params })
  .subscribe(
    result => console.log(result),
    err => console.error(err)
  );

Try the above - sorry for poor answer format, on mobile :)

Sign up to request clarification or add additional context in comments.

2 Comments

It seems like the HttpParams object isn't present in @angular/http. Where is it located? I'm running Angular 5 now and there's something about RequestOptions - not sure if it's of importance...
It comes from the new HttpClient, so import {HttpParams} from '@angular/common/http';
2

This should help:

this.http.delete(this.environment.apiUrl/+1).subscribe(data => {

});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.