我想将以下(工作)卷曲片段转换为 RestTemplate 调用:
curl -i -X POST -d "email=first.last@example.com" https://app.example.com/hr/email
如何正确传递电子邮件参数?以下代码导致 404 Not Found 响应:
String url = "https://app.example.com/hr/email";
Map<String, String> params = new HashMap<String, String>();
params.put("email", "first.last@example.com");
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response = restTemplate.postForEntity( url, params, String.class );
我试图在 PostMan 中制定正确的调用,我可以通过将电子邮件参数指定为正文中的“表单数据”参数来使其正常工作。在 RestTemplate 中实现此功能的正确方法是什么?
原文由 sim 发布,翻译遵循 CC BY-SA 4.0 许可协议
POST 方法应与 HTTP 请求对象一起发送。并且请求可能包含 HTTP 标头或 HTTP 正文或两者。
因此,让我们创建一个 HTTP 实体并在正文中发送标头和参数。
http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/client/RestTemplate.html#postForObject-java.lang.String-java.lang.Object-java.lang。类-java.lang.Object…-