Как использовать параметры httpclient и заголовки httpclient в методе httpclient post? - PullRequest
0 голосов
/ 13 мая 2019

Я хочу перевести мой угловой проект с углового 5 на угловой 7, и для этого я хочу преобразовать все HTTP-вызовы в вызов httpclient.

1 Ответ

0 голосов
/ 13 мая 2019

Не могли бы вы, пожалуйста, напишите код. Или приведите коды и коробки - пример. Вы можете использовать перехватчик для заголовка по умолчанию.

пример

 getPrices(country: string, items: string, currency: string = ''): Observable<any> {
    const params = new HttpParams()
      .set('country', country.toUpperCase())
      .set('items', items)
      .set('currency', currency.toUpperCase());
    return this.customHttpHandler.request('GET',
      `${ this.priceApiUrl }/example`,
      new PriceInterceptor(this.exampleService),
      {
        params
      }
    ).pipe(
      catchError(() => of([])),
      retry(2),
      timeout(50000)
    );
  }

Еще один пример:

export class HttpService{

    constructor(private http: HttpClient){ }

    //http://localhost:60489/Home/PostUser  ASP.NET MVC
    //http://localhost:8080/angular/setUser.php     PHP
    // http://localhost:60820/api/post
    postData(user: User){

        const myHeaders = new HttpHeaders().set('Authorization', 'my-auth-token'),;

        return this.http.post('http://localhost:60820/api/values', user, {headers:myHeaders}); 
    }
}
...