DEV Community

Cover image for How to access submitted form data value from request in ExpressJS
Adrian Matei for Codever

Posted on • Edited on • Originally published at codever.dev

2 1

How to access submitted form data value from request in ExpressJS

Project: Codever - File: user.router.js

The values are present in the request.body which contains key-value pairs of data submitted in the request body.

In our case we access the userDisplayName with the following expression request.body.userDisplayName as in the example below:

usersRouter.post('/:userId/bookmarks/upload', keycloak.protect(),
  uploadBookmarks.single("bookmarks" /* name attribute of <file> element in your form */),
  async (request, response) => {
    userIdTokenValidator.validateUserId(request);

    const userDisplayName = request.body.userDisplayName;
    const importResponse = await browserBookmarksImportService.imporBrowserBookmarks(request.params.userId, request.file.buffer, userDisplayName);

    const str = JSON.stringify(importResponse, null, 2); // spacing level = 2
    console.log(str);

    return response.status(HttpStatus.OK).send(importResponse);
  }
);
Enter fullscreen mode Exit fullscreen mode

In angular the userDisplayName value has been appended to the FormData and submitted to a post request
via the Angular Http Client:

  uploadBookmarks(userId: String, bookmarks: File, userDisplayName: string): Observable<any> {
    const formData = new FormData();
    formData.append('bookmarks', bookmarks);
    formData.append('userDisplayName', userDisplayName);

    return this.httpClient.post(`${this.usersApiBaseUrl}/${userId}/bookmarks/upload`, formData);
  }
Enter fullscreen mode Exit fullscreen mode


Reference -

https://expressjs.com/en/api.html#req.body


Shared with ❤️ from Codever. Use 👉 copy to mine functionality to add it to your personal snippets collection.

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay