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
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.
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.
Nest provides an out-of-the-box application architecture which allows developers to create highly testable,scalable, loosely coupled and easily maintainable applications.
Nest takes advantage of the latest language features, so to use it with vanilla JavaScript we need a Babel compiler.
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.
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?
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.
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.
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;
}
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));
}
}
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*/)
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With