使用HttpClient进行POST请求时发现的一个坑(收到的JSON串后面多了个“=”)
json
适用于现代 C++ 的 JSON。
项目地址:https://gitcode.com/gh_mirrors/js/json

·
今天在使用HttpClient进行POST请求调用别人的接口一直失败,查看请求报文与接口文档完全一致;最后求助于接口开发侧,抓取请求报文发现,他们接收到的JSON串后面多了个“=”号;然后通过在网上查找,问题解决。
原因:
无论是使用HttpClient进行POST,还是前端使用ajax向Java后台发送Json数据,默认请求的ContentType 是 application/x-www-form-urlencoded;charset=UTF-8 ,都会导致后台接收到的Json数据末尾多一个 “=”。
application/x-www-form-urlencoded;charset=UTF-8
是一种键值对结构,传输时将JSON内容当作了key,value为空,因此收到的报文Json的末尾多出一个等号。而Http接口提供者设置了ContentType类型,比如在Controller层的方法上添加了“@Produces("application/json;charset=UTF-8")”
解决方案:
请求时,将ContentType 改成 application/json;charset=utf-8即可解决问题。
例:
HttpPost httppost = new HttpPost(url); // 创建http post
StringEntity stringEntity = new StringEntity(data, "utf-8"); // 创建请求参数
stringEntity.setContentType("application/json");
httppost.setEntity(stringEntity);
……




适用于现代 C++ 的 JSON。
最近提交(Master分支:6 个月前 )
c67d5382
* :alembic: try matrix for latest
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
* :recycle: refactor from https://github.com/nlohmann/json/issues/4745#issuecomment-2810128420
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
* :construction_worker: simplify CI
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
* :construction_worker: simplify CI
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
* :rotating_light: fix cpplint warning
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
* :construction_worker: simplify CI
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
---------
Signed-off-by: Niels Lohmann <mail@nlohmann.me> 1 天前
88c92e60
* :rotating_light: fix warnings
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
* :rotating_light: fix warnings
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
* :alembic: enable ranges support
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
* :fire: remove ci_nvhpc job
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
* :alembic: enable ranges support
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
* :fire: remove ci_nvhpc job
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
* :rotating_light: fix warning
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
---------
Signed-off-by: Niels Lohmann <mail@nlohmann.me> 2 天前
更多推荐
所有评论(0)