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

调用JSON API时已设置Content-Type为application/json仍报415错误,如何修复?

问题:Curl设置Content-Type为application/json仍返回UNSUPPORTED_MEDIA_TYPE错误

重现环境

执行的Curl命令:

curl -i -k -X POST -H "Content-Type: application/json" -d "{\"niceSessionKey\": \"S202320220722145915340287\",\"fiCode\": \"B100000022\",\"taskCode\": \"KYC_VC2_RSLT\",\"appNumber\": \"APPL000000000000999\",\"customerNumber\": \"12345678\",\"mobilePhoneNumber\": \"0966688526\"}" https://100.113.125.128:8080/api/KYT_VC7_RSLT

服务器返回错误:

"status":"UNSUPPORTED_MEDIA_TYPE","message":"Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported","errors":["application/x-www-form-urlencoded;charset=UTF-8 media type is not supported. Supported media types are application/octet-stream, text/plain, application/xml, text/xml, application/x-www-form-urlencoded, application/*+xml, multipart/form-data, application/json, application/*+json, */*"]}

解决方法

1. 修正JSON数据的包裹方式,避免转义错误

你的命令中用双引号包裹JSON并转义内部双引号,在部分shell环境下可能导致curl无法正确识别JSON格式,转而默认使用application/x-www-form-urlencoded编码。改为用单引号包裹JSON内容,无需转义内部双引号:

curl -i -k -X POST -H "Content-Type: application/json" -d '{"niceSessionKey": "S202320220722145915340287","fiCode": "B100000022","taskCode": "KYC_VC2_RSLT","appNumber": "APPL000000000000999","customerNumber": "12345678","mobilePhoneNumber": "0966688526"}' https://100.113.125.128:8080/api/KYT_VC7_RSLT

2. 验证请求头是否真实发送

添加-v参数查看curl实际发送的请求头,确认Content-Type: application/json是否被正确传递:

curl -v -i -k -X POST -H "Content-Type: application/json" -d '{"niceSessionKey": "S202320220722145915340287","fiCode": "B100000022","taskCode": "KYC_VC2_RSLT","appNumber": "APPL000000000000999","customerNumber": "12345678","mobilePhoneNumber": "0966688526"}' https://100.113.125.128:8080/api/KYT_VC7_RSLT

查看输出中类似以下的内容,确认存在Content-Type: application/json:

> POST /api/KYT_VC7_RSLT HTTP/1.1
> Host: 100.113.125.128:8080
> User-Agent: curl/7.68.0
> Accept: */*
> Content-Type: application/json
> Content-Length: 248

3. 检查中间件是否篡改请求头

如果你的请求经过代理、负载均衡或网关,这些中间件可能会修改请求头。需要确认此类服务是否存在自动替换Content-Type的配置。

4. 验证JSON格式合法性

确保你的JSON内容格式正确,比如没有遗漏逗号、引号匹配正确。可以用jq工具验证:

echo '{"niceSessionKey": "S202320220722145915340287","fiCode": "B100000022","taskCode": "KYC_VC2_RSLT","appNumber": "APPL000000000000999","customerNumber": "12345678","mobilePhoneNumber": "0966688526"}' | jq .

如果格式正确,jq会格式化输出JSON;如果错误,会提示具体问题。

内容的提问来源于stack exchange,提问作者Khanh Luong Van

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.23 07:15:50