POST x-www-form-urlencoded

时间:2017-06-04 19:43:08

标签: java spring rest http spring-boot

我一直在阅读the following并尝试使用RestTemplate发送带有Spring Boot的POST请求。

HttpHeaders headers = new HttpHeaders();
headers.add("content-type", "application/x-www-form-urlencoded");

final String body = "clipmessage={ bridgeId: \"" + user.getBridgeId() + "\", clipCommand: { url: \"" + setLightState("3") + "\", method: \"PUT\", body: { \"on\": " + state.isOn() + " } } }";
final String url = API_ADDRESS + user.getAccessToken();

HttpEntity<String> entity = new HttpEntity<>(body, headers);

restTemplate.postForEntity(url, entity, String.class);

如果我记录了URL和正文并在邮递员中发送完全相同的内容,则会成功。但是,当我从Spring Boot应用程序发送它时,不是这样。

我猜这个特殊的身体必须以某种我不知道的特殊方式发送?

任何人都有关于接下来要尝试什么的提示?

更新1: 我按照建议尝试了MultiValueMap,但是也没有这样做。

MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();

final String body = "{ bridgeId: \"" + user.getBridgeId() + "\", clipCommand: { url: \"" + setLightState("3") + "\", method: \"PUT\", body: { \"on\": " + state.isOn() + " } } }";

map.add("clipmessage", body);

HttpEntity<String> entity = new HttpEntity<>(body, headers);

0 个答案:

没有答案