I am using the @Headers("Authorization")
annotation to inject that header into a method inside a TypeScript class, and it works as expected. However, NestJS is using this annotation and presenting it as a required header field, but then does not actually send that field in the API call.
I have main.ts
configured so that the swagger UI page has the "Authorize" button, and entering a value there causes the token to be sent correctly.
const config = new DocumentBuilder()
.setTitle("Some API")
.setDescription("The API")
.setVersion('1.0')
.addBearerAuth({
type: "http",
scheme: "bearer",
bearerFormat: "JWT",
in: "header",
name: "JWT",
description: "Enter your Bearer token",
}, "Authorization")
.addSecurityRequirements("Authorization")
.build();
const documentFactory = () => SwaggerModule.createDocument(app, config);
SwaggerModule.setup("v1/api", app, documentFactory);
However, that makes the value under Parameters
unnecessary and even wrong (since it isn't sent anyway). The screenshot below left the Authorization button at the top of the screen blank to illustrate that the value in Parameters
is not useful.
@Controller()
@Injectable()
export class UserCredentialController {
@Get(`/v1/auth/readlogin`)
async getOwnUserLoginInfo(@Headers("Authorization") authHeader: string) {
if (!authHeader) {
throw new UnauthorizedException("No authorization header found");
}
// Rest of code trimmed
}
// omitted
I have tried various combinations of @Api
annotations to no avail. Ideally there would be a way to suppress the unused Parameters
field since it's injected. What am I missing?
Posts that are similar but didn't solve the problem:
I am using the @Headers("Authorization")
annotation to inject that header into a method inside a TypeScript class, and it works as expected. However, NestJS is using this annotation and presenting it as a required header field, but then does not actually send that field in the API call.
I have main.ts
configured so that the swagger UI page has the "Authorize" button, and entering a value there causes the token to be sent correctly.
const config = new DocumentBuilder()
.setTitle("Some API")
.setDescription("The API")
.setVersion('1.0')
.addBearerAuth({
type: "http",
scheme: "bearer",
bearerFormat: "JWT",
in: "header",
name: "JWT",
description: "Enter your Bearer token",
}, "Authorization")
.addSecurityRequirements("Authorization")
.build();
const documentFactory = () => SwaggerModule.createDocument(app, config);
SwaggerModule.setup("v1/api", app, documentFactory);
However, that makes the value under Parameters
unnecessary and even wrong (since it isn't sent anyway). The screenshot below left the Authorization button at the top of the screen blank to illustrate that the value in Parameters
is not useful.
@Controller()
@Injectable()
export class UserCredentialController {
@Get(`/v1/auth/readlogin`)
async getOwnUserLoginInfo(@Headers("Authorization") authHeader: string) {
if (!authHeader) {
throw new UnauthorizedException("No authorization header found");
}
// Rest of code trimmed
}
// omitted
I have tried various combinations of @Api
annotations to no avail. Ideally there would be a way to suppress the unused Parameters
field since it's injected. What am I missing?
Posts that are similar but didn't solve the problem:
@Controller()
@ApiBearerAuth()
export class AppController {
constructor(private readonly appService: AppService) {}
@Get()
@ApiHeader({
name: 'authorization',
required: false
})
async getHello(@Headers('authorization') authHeader: string) {
if (!authHeader) {
return 'No token provided';
}
return authHeader;
}
}
add @ApiBearerAuth()
for top of the class, and try without @Injectable()