'Unable to make successful requests using nest.js HttpService

I'm trying to make a request using nest.js HttpService as specified in their docs but I keep getting an error that I can't make sense of.

I have imported the HttpModule

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';

@Module({
  imports: [HttpModule],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

and am trying to use the HttpService like this:

import { HttpService } from '@nestjs/axios';
import { Controller, Get } from '@nestjs/common';
import { lastValueFrom } from 'rxjs';
import { AppService } from './app.service';

@Controller()
export class AppController {
  constructor(
    private readonly appService: AppService,
    private httpService: HttpService
  ) {}

  @Get()
  async getHello(): Promise<string> {
    const req = this.httpService.get('https://icanhazdadjoke.com/');
    console.log(req)
    const res = await lastValueFrom(req);
    const data = res.data
    return data
  }
}

but when I query the get endpoint using curl i get an error response

http-test git:(master) ✗ curl localhost:3001
{"statusCode":500,"message":"Internal server error"}% 

and my console shows the following

[11:02:02 a.m.] File change detected. Starting incremental compilation...

[11:02:02 a.m.] Found 0 errors. Watching for file changes.

[Nest] 73798  - 2022-03-22, 11:02:03 a.m.     LOG [NestFactory] Starting Nest application...
[Nest] 73798  - 2022-03-22, 11:02:03 a.m.     LOG [InstanceLoader] HttpModule dependencies initialized +36ms
[Nest] 73798  - 2022-03-22, 11:02:03 a.m.     LOG [InstanceLoader] AppModule dependencies initialized +1ms
[Nest] 73798  - 2022-03-22, 11:02:03 a.m.     LOG [RoutesResolver] AppController {/}: +5ms
[Nest] 73798  - 2022-03-22, 11:02:03 a.m.     LOG [RouterExplorer] Mapped {/, GET} route +2ms
[Nest] 73798  - 2022-03-22, 11:02:03 a.m.     LOG [NestApplication] Nest application successfully started +2ms
Observable { _subscribe: [Function (anonymous)] }
[Nest] 73798  - 2022-03-22, 11:02:06 a.m.   ERROR [ExceptionsHandler] Request failed with status code 503
Error: Request failed with status code 503
    at createError (/Users/sv/projects/http-test/node_modules/@nestjs/axios/node_modules/axios/lib/core/createError.js:16:15)
    at settle (/Users/sv/projects/http-test/node_modules/@nestjs/axios/node_modules/axios/lib/core/settle.js:17:12)
    at IncomingMessage.handleStreamEnd (/Users/sv/projects/http-test/node_modules/@nestjs/axios/node_modules/axios/lib/adapters/http.js:322:11)
    at IncomingMessage.emit (events.js:412:35)
    at endReadableNT (internal/streams/readable.js:1317:12)
    at processTicksAndRejections (internal/process/task_queues.js:82:21)


Solution 1:[1]

A 503 is Service Unavailable. I'm able to make a call using curl and xh and it worked fine. Checking their API docs, it looks like you need to set a User-Agent header to use their API, something like

return this.
  .get('https://icanhazdadjoke.com', {
    headers: { accept: '*/*', 'User-Agent': 'NestJS Dad Joke' },
  })
  .pipe(map((res) => res.data));

ould work just fine. Check their API docs for more information

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jay McDoniel