在发送API请求时,如何保留字符串(“/quot”)作为JSONOBJECT的一部分
我试图将下面的jsonobject作为请求参数发送到REST API。
{ "date": "2022-01-01", "value": [ "TST/USED" ] }
值字段包含值列表,但是当我以此格式添加该值的一部分时,它将字符串/
替换为\/
,因此请求未处理它抛出415:[没有身体]例外。
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.add(AUTHORIZATION, "Bearer " + token);
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
JSONObject req = new JSONObject();
req.put("date", "2022-01-01");
req.put("value", "TST/USED");
HttpEntity<Object> object = new HttpEntity<>(req.toString(), headers);
Object response = restTemplate.exchange(apiUrl, HttpMethod.POST, object, Object.class)
.getBody();
I am trying to send below Jsonobject as request parameter to rest API.
{ "date": "2022-01-01", "value": [ "TST/USED" ] }
Value field contains the list of values, but when I add the value in this format as part of request it replaces string /
to \/
due to which the request is not processing and it throws 415 : [no body] exception.
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.add(AUTHORIZATION, "Bearer " + token);
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
JSONObject req = new JSONObject();
req.put("date", "2022-01-01");
req.put("value", "TST/USED");
HttpEntity<Object> object = new HttpEntity<>(req.toString(), headers);
Object response = restTemplate.exchange(apiUrl, HttpMethod.POST, object, Object.class)
.getBody();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我看不到您提到的问题,这是我正在运行的内容:
带有Spring Boot和RestTemplate Bean的主要应用程序。
测试的API:
运行您的代码的类:
当我运行代码时,我打印出
req
varible的内容:{“ date”:“ 2022-01-01”,“ value”:“ tst/paster”}
在请求往返后,我打印出响应:
{date = 2022-01-01,value = tst/aster}
这是日志:
正如您可以看到的那样,没有backsslash,也没有完成请求的问题。
I don't see the issue you mention, here is what I am running:
Main application with Spring boot and a RestTemplate bean.
An api to test:
A class to run your code:
When I run the code I print out the content of the
req
variable:{"date":"2022-01-01","value":"TST/USED"}
Also after round trip of the request I print out the response:
{date=2022-01-01, value=TST/USED}
This is the log:
As you can see there is no backslash, nor no issue for completing the request.