-1

I'm trying to get my data from my Jenkins JSON - API using HTTPClient. Because the data, I want to access is restricted I need to authenticate against Jenkins. So I generated an API-Token. Now I want to Authenticate myself using the Angular HTTPClient, but I don't know how.

Can anybody help me with my problem?

Thanks

Dan Def
  • 1,836
  • 2
  • 21
  • 39
el_diep
  • 29
  • 1
  • 7

1 Answers1

0

You can use HttpInterceptor to append additional headers on every outgoing http requests.

@Injectable({
  providedIn: 'root'
})
export class OutgoingInterceptor implements HttpInterceptor {



  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {

     interceptedRequest = req.clone({
         setHeaders: {

             // These headers is for Passport authentication.
             // You might to change these headers as you need
             // Also you need to store your token in localStorage etc.

             Accept: 'application/json',
             Authorization: tokenData
         }
     });

     return next.handle(interceptedRequest);
  }
}

And you need to add this newly created interceptor to your module's provider array:

providers: [

    ... 

    {
      provide: HTTP_INTERCEPTORS,
      useClass: AuthInterceptor,
      multi: true
    }
]
Harun Yilmaz
  • 8,281
  • 3
  • 24
  • 35