0
I’m trying to make a POST request via fetch and via Xios, but something is happening that the request arrives without the body. I used Postman to see if it could be some problem in the backend, however, by Postman the body arrives normal and without problems.
The code of the frontend:
import axios from 'axios';
import { ResultNotification } from '../../core/models/ResultNotification';
class Api {
private api = axios.create({
baseURL: 'http://localhost:3300'
});
public Post<T>(endpoint: string, body: any): Promise<ResultNotification<T>> {
let header = new Headers();
header.append('Content-Type', 'text/plain; application/json');
header.append('Accept', '*/*');
return new Promise((resolve, reject) => {
this.api.post<any, any>(endpoint, body, {
headers: {
'Content-Type': 'text/plain; application/json',
'Accept': '*/*',
}
})
// fetch(`http://localhost:3300${endpoint}`, {
// method: 'POST',
// headers: header,
// body: JSON.stringify(body),
// })
.then(response => resolve(response.data))
.catch(err => reject(err));
});
}
}
return new Api.Post<User>('/login', { username: "t", password: "t" });
The backend configured to receive the content-type and Accept header:
import { LoginController } from './app/application/controller/LoginController';
import { App, METHOD, HEADER } from 'decorated-router';
@App({
controllers: [
LoginController
],
server: {
port: 3300,
methods: [METHOD.GET, METHOD.POST],
headers: [HEADER.ORIGIN, HEADER.XREQUESTEDWITH, HEADER.CONTENTTYPE, HEADER.ACCEPT, HEADER.AUTHORIZATION]
}
})
class Loader { }
The backend receiving the body:
import { UserDto } from './../../domain/dto/UserDto';
import { Body, Controller, Post } from 'decorated-router';
import { UserService } from './../../domain/service/UserService';
@Controller({
url: '/login',
cors: '*',
auth: null
})
export class LoginController {
constructor(
private userService: UserService
) { }
@Post()
login(@Body() userDto: UserDto) {
console.log(userDto);
return this.userService.signIn(userDto);
}
@Post('/signup')
signUp(@Body() userDto: UserDto) {
return this.userService.signUp(userDto);
}
}