Content-type Header Not Handled Correctly for JSON and Form Data with Charset Information
还没有人认领这个 Issue。
评估
- 难度
- 2/5
- 预计耗时
- 1-3 小时
- 新手友好度
- 55/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 停滞
- 技术栈
- javascript, node.js
- 领域
- backend
调研方向
从 examples/middleware/bodyDecoder-middleware.js 的第 47-53 行开始,使用 Content-type: application/json; charset=utf-8 或等效的 form-data 标头形式重现请求。验证这两种带参数的内容类型都能被正确解码和序列化,同时现有的纯内容类型仍能正常工作。
由索引模型根据 Issue 内容生成。
描述
Description
In the file bodyDecoder-middleware.js, lines 47-53, the server incorrectly handles Content-type: application/json; charset=utf-8 headers and similarly formatted headers for form data (application/x-www-form-urlencoded). The existing code uses strict string comparison, causing issues when the Content-type header contains additional properties like charset.
Existing Code
// bodyDecoder-middleware.js, lines 47-53
if (contentType === 'application/json') {
bodyData = JSON.stringify(req.body);
}
if (contentType === 'application/x-www-form-urlencoded') {
bodyData = queryString.stringify(req.body);
}
This code only works for requests that send a pure Content-type: application/json or Content-type: application/x-www-form-urlencoded without any additional properties like charset.
Expected behavior
The code should be able to handle Content-type headers with additional parameters.
Standard Practice According to MDN
According to MDN's documentation on Content-Type, the header can contain additional parameters (like charset), separated by a semicolon. Therefore, using startsWith would align better with this standard practice.
Solution
A possible solution is to change the string comparison to check if the Content-type starts with the desired value. Below is the proposed change:
if (contentType.startsWith('application/json')) {
bodyData = JSON.stringify(req.body);
}
if (contentType.startsWith('application/x-www-form-urlencoded')) {
bodyData = queryString.stringify(req.body);
}
Steps to Reproduce
- Send a POST request with a
Content-type: application/json; charset=utf-8header and JSON payload. - Observe that the existing code does not handle the body as expected.
Thank you for taking the time to look into this issue.
- 主要语言
- JavaScript
- 星标
- 14.1k
- 派生
- 2k
- PR 合并指标
- 30 天内没有已合并 PR
环境准备
这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
http-party/node-http-proxy 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 78/100
http-party/node-http-proxy#1719 ·
-
难度 3/5 1-2 天 新手友好度 72/100
http-party/node-http-proxy#1721 · 1 条评论 ·
-
难度 1/5 1 小时以内 新手友好度 55/100
http-party/node-http-proxy#1715 · 1 条评论 · 5 个 reaction ·
-
难度 1/5 1 小时以内 新手友好度 15/100
http-party/node-http-proxy#1711 · 2 条评论 ·
-
难度 5/5 一周以上 新手友好度 25/100
http-party/node-http-proxy#1710 · 5 个 reaction ·
查看 http-party/node-http-proxy 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 78/100
guybedford/es-module-lexer#266 ·
-
bug documentation
难度 1/5 1-3 小时 新手友好度 88/100
apify/apify-docs#3206 ·
维护者通常 1 天内回复
-
enhancement good first issue
难度 2/5 1-3 小时 新手友好度 90/100
anhnt256/cursor-swarm#21 ·
-
难度 2/5 1-3 小时 新手友好度 72/100
-
documentation good first issue
难度 2/5 1-3 小时 新手友好度 74/100
zerohourzulu/irontrellis#16 ·