title: Send headers through generic method on HTTP CRUD workflow
body: <p>I have a service like this:</p>
  
  <p><em>Note:</em> Here I need to work with the <code>cookies</code></p>
  
  <pre><code>  book(data: Spa): Observable&lt;any&gt; {
      return this.http.post(`${environment.apiURL}:${environment.port}/${environment.domain}/abc/my.json`, data,
        {
          headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
        });
    }
  </code></pre>
  
  <p>You can see that each and every method where I have to send <code>headers</code>. Which is very ugly and not <code>DRY</code>. So I would like to write generic <code>CRUD</code> to add it automatically. How can I do that?</p>
  
  <p>I have tried this:</p>
  
  <pre><code>import { Injectable } from '@angular/core';
  import { Observable } from 'rxjs';
  import { HttpClient } from '@angular/common/http';
  import { RequestOptions } from '@angular/http';
  
  @Injectable({
    providedIn: 'root'
  })
  export class CommonService {
  
    constructor(private http: HttpClient) { }
  
    post(url, params): Observable&lt;any&gt; {
      return new Observable(observer =&gt; {
        const headers = new Headers();
        let options = new RequestOptions({ headers: this.createHeader(headers) });
        this.http.post(url, params, options)
          .subscribe(response =&gt; {
            observer.next(response);
            observer.complete();
          }, (e) =&gt; {
            observer.error(e);
          });
  
      })
    }
  
    createHeader(headers: Headers): Headers {
      headers.append('Content-Type', 'application/x-www-form-urlencoded');
      return headers;
    }
  }
  </code></pre>
  
  <p>But it gives this <code>compile time</code> error on this line <code>let options = new RequestOptions({ headers: this.createHeader(headers) });</code></p>
  
  <blockquote>
    <p>Type 'Headers' is missing the following properties from type
    'Headers': keys, values, toJSON, getAll, and 2 more.ts(2740)
    interfaces.d.ts(61, 5): The expected type comes from property
    'headers' which is declared here on type 'RequestOptionsArgs'
    (property) RequestOptionsArgs.headers?: Headers</p>
  </blockquote>
  
  <p>And it says this <code>warning</code> too:</p>
  
  <blockquote>
    <p>RequestOptions is deprecated: see <a href="https://angular.io/guide/http" rel="nofollow noreferrer">https://angular.io/guide/http</a>
    (deprecation)</p>
  </blockquote>