代码之家  ›  专栏  ›  技术社区  ›  Svelt

无法使用nest成功发出请求。js HttpService

  •  0
  • Svelt  · 技术社区  · 2 年前

    我正试图用nest发出请求。js HttpService是在他们的文档中指定的,但我一直收到一个我无法理解的错误。

    我已经导入了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 {}
    

    我正试图像这样使用HttpService:

    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
      }
    }
    

    但当我使用curl查询get端点时,会得到一个错误响应

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

    我的控制台显示如下

    [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)
    
    1 回复  |  直到 2 年前
        1
  •  1
  •   Jay McDoniel    2 年前

    503服务不可用。我可以用手机打电话 curl xh 而且效果很好。查看他们的API文档,看起来你需要设置一个 User-Agent 头来使用他们的API,比如

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

    你会做得很好的。 Check their API docs for more information