Express:req.params与req.body(JSON)哪种更规范?场景与标准探讨
Which Approach is "Correct" for Getting a User ID in Express?
Great question! Let’s break this down clearly—neither method is inherently "wrong," but which one you should use depends entirely on your API’s purpose, REST conventions, and what you’re trying to accomplish. Let’s dive into the details:
1. Using req.body.userId (POST with JSON payload)
- Best for: Creating a new user, or when you’re sending multiple pieces of data alongside the user ID (like profile info, settings, etc.).
- The reasoning: Request bodies are made for carrying payload data—stuff that’s being created or updated. If your
/userendpoint exists to register a new user, it makes total sense to include the user ID (along with name, email, etc.) in the body. - Example scenario: Your
POST /userendpoint handles new user signups. The request body might look like this:{ "userId": "usr_12345", "name": "Jane Smith", "email": "jane@example.com" } - Word of caution: Don’t use this for simple lookup operations. Sending a user ID in the body just to fetch a user’s data goes against standard REST norms—URLs should tell you which resource you’re targeting.
2. Using req.params.userId (POST with URL parameter)
- Best for: Operations that target a specific existing user—like updating their profile, triggering an action for them, or fetching their specific data.
- The reasoning: URL parameters are designed to identify a specific resource in your API. REST conventions say URLs should represent resources, so
POST /user/:userIdimmediately signals that this request is tied to the user with ID:userId. - Example scenario: Your
POST /user/:userId/verifyendpoint confirms a user’s email. The URL tells you exactly which user to verify, and the body might include a verification token (if needed). - Word of caution: Don’t overcrowd your URL with parameters. Keep it focused on identifying the core resource you’re working with.
Are There Official Standards?
Absolutely! REST architectural guidelines (and widely adopted API design best practices) give clear direction:
- URLs = Resource identifiers: Use path parameters (
req.params) to specify which resource you’re interacting with. - Request body = Payload data: Use
req.bodyfor data that’s being created, updated, or sent as input for the operation.
For quick reference, here’s how this plays out in common scenarios:
GET /user/:userId(fetch a specific user) → Usereq.params.userIdPOST /user(create a new user) → Usereq.bodyfor all user data (including ID)PUT /user/:userId(update a user’s profile) → Usereq.params.userIdto identify the user, andreq.bodyfor the updated details
Final Takeaway
To decide which approach to use, ask yourself one key question:
- Am I targeting a specific existing user? Go with
req.params.userId. - Am I sending new data to create a user or including multiple details alongside the ID? Go with
req.body.userId.
Neither method is "more correct"—it’s all about aligning your endpoint’s purpose with standard API design patterns to keep your code intuitive and maintainable.
内容的提问来源于stack exchange,提问作者chrisheseltine
相关产品推荐
相关产品推荐

