Skip to content

HTTP

This library allows us to make http requests. Its core is Axios.

Install

Terminal window
npm install @zanobijs/http

Setting

We add the http module in main module imports

import { Module } from "@zanobijs/common";
import { HttpModule } from "@zanobijs/http";
import { ControllerExample } from "./example.controller";
import { ServiceExample } from "./example.service";
@Module({
imports: [HttpModule],
controllers: [ControllerExample],
services: [ServiceExample],
exports: [],
})
export class AppModule {}

Then we use it in our service

import { Injectable } from "@zanobijs/common";
import { HttpClient } from "@zanobijs/http/client";
@Injectable()
export class ServiceExample {
constructor(
private httpClient: HttpClient
) {}
async getDataAPI(){
return await this.httpClient.get(
"https://jsonplaceholder.typicode.com/posts/1",
{
headers: {
"Content-type": "application/json; charset=UTF-8",
}
}
);
}
}

In addition to the above, you can use the reduceResponse: true property in the options to reduce the answer and get only:

{
status: 200,
statusText: 'OK',
config: {
url: '...',
method: 'get',
headers: Object [AxiosHeaders] {
Accept: '...',
Content-Type: '...',
'User-Agent': '...',
'Accept-Encoding': '...'
}
},
data: {...}
}