Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nestjs using axios

This simple demo has an error https://docs.nestjs.com/techniques/http-module

import { Get, Controller, HttpService } from '@nestjs/common';
import { AxiosResponse } from 'axios'
import { Observable } from 'rxjs'
@Controller()
export class AppController {
  constructor(private readonly http: HttpService) {}
  @Get()
  root(): Observable<AxiosResponse<any>>  {
    return this.http.get('https://api.github.com/users/januwA');
  }
}

What should I do?

[Nest] 7356   - 2018-10-18 00:08:59   [ExceptionsHandler] Converting circular structure to JSON +9852ms
TypeError: Converting circular structure to JSON
    at JSON.stringify (<anonymous>)

nest i
common version : 5.1.0
core version   : 5.1.0
like image 794
januw a Avatar asked Oct 17 '18 16:10

januw a


People also ask

Can I use Axios in NestJS?

Axios is richly featured HTTP client package that is widely used. Nest wraps Axios and exposes it via the built-in HttpModule . The HttpModule exports the HttpService class, which exposes Axios-based methods to perform HTTP requests.

How does NestJS call external API?

I believe what you need to do is create a Service, put your example code into a function, then call that function from a controller or resolver with DI to the Service you just created. please see sample code below for your reference. can also use HttpModule instead of raw axios as per the nestjs documentation.

Is NestJS scalable?

Nest provides an out-of-the-box application architecture which allows developers to create highly testable,scalable, loosely coupled and easily maintainable applications.

Does NestJS use Babel?

Nest takes advantage of the latest language features, so to use it with vanilla JavaScript we need a Babel compiler.

What is the use of Axios in nest?

Axios is richly featured HTTP client package that is widely used. Nest wraps Axios and exposes it via the built-in HttpModule. The HttpModule exports the HttpService class, which exposes Axios-based methods to perform HTTP requests.

What is Axios in Node JS?

Axios is a promise-based HTTP client for the browser and Node.js. It's able to run both on the browser and server (unlike specific browser APIs like fetch ). If you did want to use fetch inside of Node.js, you could use node-fetch which is a module that wraps the fetch API for Node.js. Why should I use Axios?

What is @nestjs?

A progressive Node.js framework for building efficient and scalable server-side applications. Axios module for Nest originally published as part of the @nestjs/common package. This package is a drop-in replacement for deprecated HttpModule. Nest is an MIT-licensed open source project.

Should I use Axios to make HTTP requests?

Chances are, you'll need to make HTTP requests in your web applications. There's numerous options for this, including native browser APIs such as fetch, but Axios is often used when you want more power and extensibility. What is Axios? Axios is a promise-based HTTP client for the browser and Node.js.


3 Answers

You cannot just return the whole AxiosResponse object because it cannot be serialized to JSON. You most likely want to get the response data like this:

@Get()
root() {
  return this.http.get('https://api.github.com/users/januwA').pipe(
    map(response => response.data)
  );
}

or alternatively using Promises:

@Get()
async root() {
  const response = await this.http.get('https://api.github.com/users/januwA').toPromise();
  return response.data;
}
like image 56
Kim Kern Avatar answered Oct 20 '22 13:10

Kim Kern


as you write in your example, get method return AxiosResponse<> and contains circular reference. So if you want to proxify webservice https://api.github.com/users/januwA, you should return AxiosResponse.data :

import { Get, Controller, HttpService } from '@nestjs/common';
import { AxiosResponse } from 'axios'
import { Observable } from 'rxjs'
@Controller()
export class AppController {
  constructor(private readonly http: HttpService) {}
  @Get()
  root(): Observable<any>{
    return this.httpClient.get('https://api.github.com/users/quen2404')
      .pipe(map(response => response.data));
  }
}
like image 41
quen2404 Avatar answered Oct 20 '22 12:10

quen2404


You have to make sure to handle your responses as a JSON you can return it as a promise and get the data, use one of both or HttpService or axios

import { Get, Controller, HttpService } from '@nestjs/common';
@Controller()
export class AppController {
  constructor(private readonly http: HttpService) {}
  @Get()
      root(): {
        return this.http.get('https://api.github.com/users/quen2404')
        .toPromise()
        .then(res => res.data)
        .catch(err => /*handle error*/)
      }
}
like image 3
Juan Velasquez Avatar answered Oct 20 '22 14:10

Juan Velasquez