如何在httpclient post方法中使用httpclient参数和httpclient标头?

时间:2019-05-13 11:10:31

标签: angular angular-httpclient

我想将我的角度项目从角度5迁移到角度7,为此,我想将所有HTTP调用都转换为httpclient调用。

1 个答案:

答案 0 :(得分:-1)

请问,编写代码。或提供一个codeandbox-示例。 您可以使用拦截器作为默认标题。

一些例子

 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}); 
    }
}