spring-boot - Http 400 使用 resttemplate postforobject
问题描述
下面是给出 400 错误请求的代码,尝试了各种选项,但总是以 400 错误请求结束,但是使用 OkHttp 客户端同样有效
HttpHeaders hh = new HttpHeaders();
RestTemplate rt = new RestTemplate();
String reqBody = new ObjectMapper.writeValueAsString(“test msg”);
headers.set(“Accept”, “text/plain”);
headers.set(“content-type”,”application/json”);
headers.set(“authorization”, “Basic xxxxx”);
headers.set(“ibm-mq-rest-csrf-token”,”blank”);
HttpEntity<String> request = new HttpEntity<>(reqBody.toString());
ResponseEntity<Object> result = rt.postForObject(url, request, Object.class);
解决方案
您的内容似乎不是 JSON,在这两种情况下它似乎都是纯文本。因此标题
Content-Type: text/plain
应该使用而不是
Content-Type: application/json
我不确定为什么这在您的 OKHTTP 示例中有效,除非您用来构建帖子正文的媒体类型以某种方式覆盖了您的Content-Type
标题?
推荐阅读
- python - is it safe and/or pythonic to directly overwrite dict entries in the constructor?
- scala - 使用 akka 持久性进行批量操作,有哪些选择?
- javascript - React Redux - 当子组件过滤器字段不再匹配时,React 侧过滤在编辑时卸载子组件
- typescript - 测试 Observable 的输出
- dataframe - Pyspark:基于所有列减去/差异 pyspark 数据帧
- currying - GNU APL 中的柯里化
- c# - EF Core 5 是否引入了新的 QueryRootExpression 概念?
- javascript - 为什么在javascript文件中未定义“this”
- javascript - 使用 lodash 展平嵌套对象/数组
- selenium - 在 selenium Java 中避免卡巴斯基警报“连接不受保护”