ADAMJR
ADAMJR

Reputation: 2738

Angular 9 - PUT requests are not sending the body

// any component
async submit(value: any) {
  await this.guildService.saveGuild(this.guild.id, value);
}
...
// guild.service.ts
saveGuild(id: string, value: any) {
  return this.http.put(`${this.endpoint}/${id}?key=${this.key}`, value).toPromise();
}

API route

router.put('/:id', async (req, res) => {
    try {        
        const id = req.params.id;
        validateGuildManager(req.query.key, id);

        const updatedGuild = await SavedGuild.findByIdAndUpdate(id, req.body).lean();

        res.json(updatedGuild);
    } catch { res.status(400).send('Bad Request'); }
});

I've tried many different value combinations but the body is undefined on the API.

Repo: https://github.com/theADAMJR/2pg-dashboard - commit not included, but issue persists

Upvotes: 0

Views: 47

Answers (1)

Michael
Michael

Reputation: 2454

Sounds like you don't have a body parser middleware. Without using a body parser body will always be null/undefined.

The angular side looks fine to me, you can verify by looking at the request in the browser network requests tab and observing that the request body is present

For express try https://expressjs.com/en/resources/middleware/body-parser.html

For koa try https://www.npmjs.com/package/koa-bodyparser

Upvotes: 2

Related Questions