ColdFusion 2021用cfhttp发起PATCH请求时Content-Type被篡改如何解决
ColdFusion 2021 cfhttp PATCH请求Content-Type异常修复方案
问题根因
ColdFusion 2021的cfhttp对PATCH方法的内置处理存在特定逻辑:当框架未识别到明确的body类型请求参数时,会默认将请求判定为表单提交场景,自动覆盖自定义的Content-Type为application/x-www-form-urlencoded。你当前直接将序列化后的JSON写在cfhttp标签内部的写法,无法被框架识别为合法请求体,因此触发了默认的类型覆盖逻辑。
修复方案
方案1:直接使用PATCH方法(推荐)
核心修正点:
- 删除直接写在cfhttp标签内的JSON序列化代码,改用
type="body"的cfhttpparam传递请求体,注意该参数不需要加name属性 - 删除无效的CGI类型参数、Mimetype自定义header,仅保留标准的Content-Type header
修正后代码如下:
<cfhttp url="#loc.URLInsertID#" method="PATCH" timeout="999" result="loc.AssetPandaGetObjectDataUpdate" charset="utf-8"> <cfhttpparam type="header" name="Accept" value="application/json" /> <cfhttpparam type="header" name="Content-Type" value="application/json; charset=utf-8" /> <cfhttpparam type="header" name="Authorization" value="#Bearer#"> <cfhttpparam type="body" value="#SerializeJSON(loc.bodyElements)#"> </cfhttp>
方案2:POST方法兼容方案(部分网关限制PATCH时使用)
如果服务端网关限制直接接收PATCH请求,可使用方法覆写逻辑,注意同样要正确传递body参数:
<cfhttp url="#loc.URLInsertID#" method="POST" timeout="999" result="loc.AssetPandaGetObjectDataUpdate" charset="utf-8"> <cfhttpparam type="header" name="Accept" value="application/json" /> <cfhttpparam type="header" name="Content-Type" value="application/json; charset=utf-8" /> <cfhttpparam type="header" name="X-HTTP-Method-Override" value="PATCH" /> <cfhttpparam type="header" name="Authorization" value="#Bearer#"> <cfhttpparam type="body" value="#SerializeJSON(loc.bodyElements)#"> </cfhttp>
验证方式
请求完成后可输出loc.AssetPandaGetObjectDataUpdate.requestHeader,查看实际发送的Content-Type是否符合预期。
内容的提问来源于stack exchange,提问作者JLunceford
相关产品推荐
相关产品推荐

