Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular - OpenApi CodeGen - How to add Header in a api request?

I'm working with Angular 10. I'm using openapi codegen (auto-generated) services.

Issue: I want to pass Authorization: Token 123456 in my header but can't seem to get my head around how and where exactly to add the interceptor.

My Component File is:

 constructor(        
    private meApi : MeService
    )


 meDetails;
  meCheck(){
      this.meApi.meRead().subscribe(
        (data: any) => {
          this.meDetails= data;
        },
        (err) => { console.error(err); },
        () => { }
      )
    }

Upon running this function in ngOnInIt. I get the following error:

401 (Unauthorized)

Because obviously, the authorization token is not passed with the API.

meRead() function from the auto generated service file has the following relevant code:

 /**
     * @param observe set whether or not to return the data Observable as the body, response or events. defaults to returning the body.
     * @param reportProgress flag to report request and response progress.
     */
    public meRead(observe?: 'body', reportProgress?: boolean, options?: {httpHeaderAccept?: 'application/json'}): Observable<User>;
    public meRead(observe?: 'response', reportProgress?: boolean, options?: {httpHeaderAccept?: 'application/json'}): Observable<HttpResponse<User>>;
    public meRead(observe?: 'events', reportProgress?: boolean, options?: {httpHeaderAccept?: 'application/json'}): Observable<HttpEvent<User>>;
    public meRead(observe: any = 'body', reportProgress: boolean = false, options?: {httpHeaderAccept?: 'application/json'}): Observable<any> {

        let headers = this.defaultHeaders;

        // authentication (DRF Token) required
        if (this.configuration.apiKeys) {
            const key: string | undefined = this.configuration.apiKeys["DRF Token"] || this.configuration.apiKeys["Authorization"];
            if (key) {
                headers = headers.set('Authorization', key);
            }
        }

        let httpHeaderAcceptSelected: string | undefined = options && options.httpHeaderAccept;
        if (httpHeaderAcceptSelected === undefined) {
            // to determine the Accept header
            const httpHeaderAccepts: string[] = [
                'application/json'
            ];
            httpHeaderAcceptSelected = this.configuration.selectHeaderAccept(httpHeaderAccepts);
        }
        if (httpHeaderAcceptSelected !== undefined) {
            headers = headers.set('Accept', httpHeaderAcceptSelected);
        }

How can I pass the Authorization: Token 12345 in the headers of this request?

Summary: How to pass Authorization header in openapi codegen service file?

Will really appreciate if you could give a detailed answer. Thank You.

like image 274
String Name Avatar asked Sep 08 '26 22:09

String Name


1 Answers

on app.modules

    import { environment } from './environments/environment'

something like

    const configurationFactory = () => {
        const configParams: ConfigurationParameters = {
            credentials : {
                apiKeys : environment.apiKeys["DRF Token"] ,
            }
        }
        return new Configuration(configParams)
    } 

and on same file on imports

    ApiModule.forRoot(configurationFactory)

your environment file should follow that so you have to add those there something like:

    export const environment = {
        apiKeys : {
           "DRF Token" : 'yourkey',
           "Authorization" : 'yourauth'
         }
    }
like image 93
user21679679 Avatar answered Sep 11 '26 13:09

user21679679