Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use http delete () in Angular 6

myService.ts

  import { Injectable } from '@angular/core';
  import { Http, Response,Headers,RequestOptions } from '@angular/http';
  import { Observable } from 'rxjs';
  import 'rxjs/Rx';
  import{Router} from'@angular/router';
  import 'rxjs/add/operator/map';
  @Injectable()
  export class ManageJobDeleteService{
    constructor(private http: Http,private router:Router){};
    DeleteRequestToAPI(post_jobs_id:string){
       let body = JSON.stringify({post_jobs_id:post_jobs_id});
       let headers = new Headers({ 'Content-Type': 'application/json'});
       let options = new RequestOptions({ headers: headers });
        console.log(body);
        console.log(options);
      return this.http.delete('http://127.0.0.1:8000/api/employer/post_jobs/',body,options)//HERE I AM GETTING ERROR
      .map(this.extractData)
      .catch(this.handleError);
    }
   private extractData(res: Response){
      let body = res.json();
      console.log(body);
      return body || [];   
    }
    private handleError (error: any) {
      console.error(error);
      return Observable.throw(error.json().error || 'Server error');
    }
  }

ERROR:

Expected 1-2 arguments, but got 3.ts(2554).

Please tell me how to pass both body and options using http delete request in Angular 6.

like image 729
lpd Avatar asked Dec 19 '18 11:12

lpd


People also ask

What is delete in angular?

The delete method is used to delete the resource from a server. Delete request makes a change in the server state. Sending the message on a delete request might cause some services to reject the request but you still send the data to the server using URL Parameter.

Which HTTP method is used to delete data?

The HTTP DELETE method is used to delete a resource from the server. Unlike GET and HEAD requests, the DELETE requests may change the server state. Sending a message body on a DELETE request might cause some servers to reject the request.


2 Answers

there is no body argument on delete.

so, you need to remove body argument.

this.http.delete('http://127.0.0.1:8000/api/employer/post_jobs/',options)

Reference

class HttpClient {
     delete(url: string, 
       options: { headers?: HttpHeaders | { [header: string]: string | string[]; }; 
       observe?: HttpObserve; params... = {}): Observable<any>
}

Edit: How can I pass post_jobs_id?

you can use HttpParams:

let httpParams = new HttpParams().set('aaa', '111');
httpParams.set('bbb', '222');

let options = { params: httpParams };

this.http.delete('http://127.0.0.1:8000/api/employer/post_jobs/',options);
like image 51
Derviş Kayımbaşıoğlu Avatar answered Sep 16 '22 15:09

Derviş Kayımbaşıoğlu


What about defining your post_jobs_id in your url? And after that get the id in your backend.

For example, you can define :

DeleteRequestToAPI(post_jobs_id:string) {
    const url = `http://127.0.0.1:8000/api/employer/post_jobs/${post_jobs_id}`;
    return this.http.delete(url, {
        headers: new HttpHeaders({ 'Content-Type': 'application/json' })
    })
    .map(this.extractData)
    .catch(this.handleError);
}
like image 45
Johan Rin Avatar answered Sep 17 '22 15:09

Johan Rin