I'm facing an issue retrieving PUT requests from a service within my Angular application. The problem lies in the fact that I cannot access the data from the request's body as it returns undefined. Below is the code snippet for reference.
ANGULAR
setSessionName(sessionId: number, newSessionName: string): Observable<Session> {
const body = {
sessionId: sessionId,
newName: newSessionName
};
return this.httpClient.put<Session>(`${this.baseUrl}/set-session-name`, body);
}
SERVER
const express = require('express');
const cors = require('cors');
const serverPort = 8080;
const app = express();
app.use(cors({
origin: ['http://localhost:4200']
}));
app.put('/set-session-name', (httpRequest, httpResponse) => {
console.log(httpRequest.body); // Output: undefined
httpResponse.sendStatus(200);
});
app.listen(serverPort, () => {
console.log(`Server running at http://127.0.0.1:${serverPort}`);
})