我正在构建一个Spring Boot服务,它应该调用外部API,然后将获取的数据发送到客户端。要发送任何请求,我必须首先通过发送包含请求正文中特定数据的POST请求进行身份验证,然后该请求将包含身份验证密钥的响应作为cookie返回。我已经在Postman中让它工作了,但不知道如何在Spring Boot中做到这一点。
发布于 2021-06-09 15:28:02
我建议使用类似的东西:主要的ideea是使用rest模板。
RestTemplate restTemplate = new RestTemplate();
HttpEntity entity = new HttpEntity<String>(headers);
try {
String result = restTemplate.postForEntity(externalAPIUrl, entity, String.class).getBody();
} catch (HttpClientErrorException ex) {
if (ex.getStatusCode() == HttpStatus.BAD_REQUEST) {
}
}https://stackoverflow.com/questions/67894508
复制相似问题