Search code examples
node.jsnestjsnest

NestJS Controller always return 201 created when it works with httpclient


I'm new to the NestJS. I want to make my service to connect with 3rd party API. When I try to call my API, it always return 201 Created with no result in http body,

Here is my service,

@Injectable()
export class AuthService {
  constructor(
    private readonly httpService: HttpService, 
    private configService: ConfigService
  ) {}

  authWithHrService(authDto:AuthDto): Observable<any> {
    const host = this.configService.get<string>('HR_HOST');
    const port = this.configService.get<string>('HR_PORT');
    return this.httpService.post(`${host}:${port}/hr/authen`, authDto).pipe(
      map(response => response.data)
    );

  }

}

And here is my Controller (which always return 201),

@Controller('auth')
export class AuthController {
  private readonly logger = new Logger();
  constructor(private readonly authService: AuthService) {}

  @Post('')
  authen(@Body() req: AuthDto) {

    this.authService.authWithHrService(req).subscribe({
      next: (data) => {
        this.logger.log(`---> authResult :: ${JSON.stringify(data)}`); //This log show result successfully.
        return data;
      },
      error: (err) => {
        throw new HttpException("Authentication Failed.", HttpStatus.UNAUTHORIZED);
      }
    });

  }

}

Based on the Controller, even the result will be in the error case, it still returns 201 Created instead of 401

enter image description here

Please help.


Solution

  • You don't return anything from the controller, and you don't use the library-specific approach so Nest will see the method call was successful. If you want Nest to wait, either return the observable (without subscribing to it) or inject @Res() and handle sending res.status().send() yourself if you want to keep subscribing yourself