RestTemplate POST 表单 URL 编码数据给出 400 错误

发布于 2025-01-12 21:19:23 字数 2311 浏览 1 评论 0原文

我正在使用 Spring(版本 2.5.4)RESTTemplate 对 REST 端点 (url) 进行 POST 调用。这是我的代码:

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
    MultiValueMap<String,String> requestBody = new LinkedMultiValueMap<>();
    requestBody.add("token", "abc");
    HttpEntity<MultiValueMap<String,String>> request = new HttpEntity<>(requestBody, headers);
    try
    {       
        ResponseEntity<TokenValidationResponse> responseEntity = restTemplate.exchange(url, HttpMethod.POST, request, TokenValidationResponse.class);   
        TokenValidationResponse response = responseEntity.getBody();
        Boolean isActive = response.getActive();
        if(isActive == null || !isActive) {
            log.info("The token supplied isn't active");
        }
    }
    catch(RestClientException e)
    {
        log.error("An error occurred while posting query to security URI for token validation", e);
    }

我不断遇到 400 错误请求 -

org.springframework.web.client.HttpClientErrorException$BadRequest: 400 Bad Request: [{"error_description":"token parameter is required for the security endpoint.","error":"invalid_request"}]
    at org.springframework.web.client.HttpClientErrorException.create(HttpClientErrorException.java:101)
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:186)
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:125)
    at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63)
    at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:819)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:777)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:711)
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:602)

这是对安全端点 url 给出的要求 - 此端点仅接受 HTTP POST 方法。所需的 Content-Type 值为 application/x-www-form-urlencoded。该端点具有以下参数: token

我可以看到令牌正在传入。当我提供令牌作为正文的一部分时,我能够验证该调用在 POSTMAN 上是否正常工作。我想知道我是否缺少什么?

I'm using Spring's (version 2.5.4) RESTTemplate to make a POST call to a REST end point (url). Here is the code I have:

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
    MultiValueMap<String,String> requestBody = new LinkedMultiValueMap<>();
    requestBody.add("token", "abc");
    HttpEntity<MultiValueMap<String,String>> request = new HttpEntity<>(requestBody, headers);
    try
    {       
        ResponseEntity<TokenValidationResponse> responseEntity = restTemplate.exchange(url, HttpMethod.POST, request, TokenValidationResponse.class);   
        TokenValidationResponse response = responseEntity.getBody();
        Boolean isActive = response.getActive();
        if(isActive == null || !isActive) {
            log.info("The token supplied isn't active");
        }
    }
    catch(RestClientException e)
    {
        log.error("An error occurred while posting query to security URI for token validation", e);
    }

I keep running into a 400 bad request -

org.springframework.web.client.HttpClientErrorException$BadRequest: 400 Bad Request: [{"error_description":"token parameter is required for the security endpoint.","error":"invalid_request"}]
    at org.springframework.web.client.HttpClientErrorException.create(HttpClientErrorException.java:101)
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:186)
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:125)
    at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63)
    at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:819)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:777)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:711)
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:602)

This is the requirement given for the security endpoint url - This endpoint accepts only the HTTP POST method. The required Content-Type value is application/x-www-form-urlencoded. The endpoint has the following parameters: token

I can see the token being passed in though. I am able to verify that the call works fine on POSTMAN when I supply the token as part of the body. I was wondering if there is something I am missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

套路撩心 2025-01-19 21:19:23

您需要在标头中指定令牌,例如:

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("Authorization", "Bearer "+accessToken);

我建议查看此有用的

You need to specify the token in the headers for example:

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("Authorization", "Bearer "+accessToken);

I suggest to see this helpfull post

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文