typescript 角度HTTP设置授权标头

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了typescript 角度HTTP设置授权标头相关的知识,希望对你有一定的参考价值。

import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';

@Injectable()
export class PostService {

  constructor(private http:HttpClient) { }

  getPosts() {
    console.log('cuba panggil API');
    return this.http.get('http://restapi.jomphp.com/wp-json/wp/v2/posts');
  }

  createPost(data) {
    console.log('cuba panggil create post API');

    //set authorization headers

    const token = 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC9yZXN0YXBpLmpvbXBocC5jb20iLCJpYXQiOjE1Mjk4MTQzMTMsIm5iZiI6MTUyOTgxNDMxMywiZXhwIjoxNTMwNDE5MTEzLCJkYXRhIjp7InVzZXIiOnsiaWQiOiIxIn19fQ.6cKDCcoP7pdUajTiEdWqhQmkwRat8zBQe3bfBK2gNqk';

    const headers = new HttpHeaders()
      .set('Authorization', token);

    return this.http.post('http://restapi.jomphp.com/wp-json/wp/v2/posts', data, { headers });
  }

}

以上是关于typescript 角度HTTP设置授权标头的主要内容,如果未能解决你的问题,请参考以下文章

带有授权令牌标头的角度 $http GET 请求

HTTP标头设置授权标头阻止数据加载

Angularjs设置授权标头

基本 HTTP 授权标头

Angularjs设置了授权标头

AWS API Gateway + Cognito 用户池授权方 + Lambda - 我需要设置哪些 HTTP 标头和权限?