You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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 /user endpoint 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 /user endpoint 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/:userId immediately signals that this request is tied to the user with ID :userId.
  • Example scenario: Your POST /user/:userId/verify endpoint 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.body for 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) → Use req.params.userId
  • POST /user (create a new user) → Use req.body for all user data (including ID)
  • PUT /user/:userId (update a user’s profile) → Use req.params.userId to identify the user, and req.body for 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 04:08:52