Java 带有 URL 编码数据的 Spring RestTemplate POST 请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49127240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 02:58:16  来源:igfitidea点击:

Spring RestTemplate POST Request with URL encoded data

javaspringspring-web

提问by Tobi

I'm new to Spring and trying to do a rest request with RestTemplate. The Java code should do the same as below curl command:

我是 Spring 的新手,试图用 RestTemplate 做一个休息请求。Java 代码应与以下 curl 命令执行相同的操作:

curl --data "name=feature&color=#5843AD" --header "PRIVATE-TOKEN: xyz" "https://someserver.com/api/v3/projects/1/labels"

But the server rejects the RestTemplate with a 400 Bad Request

但是服务器拒绝了 RestTemplate 400 Bad Request

RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.add("PRIVATE-TOKEN", "xyz");
HttpEntity<String> entity = new HttpEntity<String>("name=feature&color=#5843AD", headers);
ResponseEntity<LabelCreationResponse> response = restTemplate.exchange("https://someserver.com/api/v3/projects/1/labels", HttpMethod.POST, entity, LabelCreationResponse.class);

Can somebody tell me what I'm doing wrong?

有人可以告诉我我做错了什么吗?

回答by VijayD

You need to set the Content-Type to application/json. Content-Type has to be set in the request. Below is the modified code to set the Content-Type

您需要将 Content-Type 设置为 application/json。必须在请求中设置 Content-Type。下面是修改后的代码来设置 Content-Type

final String uri = "https://someserver.com/api/v3/projects/1/labels";
String input = "US";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
headers.add("PRIVATE-TOKEN", "xyz");
HttpEntity<String> request = new HttpEntity<String>(input, headers);
ResponseEntity<LabelCreationResponse> response = restTemplate.postForObject(uri, request,  LabelCreationResponse.class);

Here, HttpEntity is constructed with your input i.e "US" and with headers. Let me know if this works, if not then please share the exception. Cheers!

在这里,HttpEntity 是用您的输入(即“US”)和标头构造的。让我知道这是否有效,如果无效,请分享异常。干杯!

回答by Hades

It may be a Header issue check if the header is a Valid header, are u referring to "BasicAuth" header?

如果标头是有效标头,则可能是标头问题,您指的是“BasicAuth”标头吗?

HttpHeader headers = new HttpHeaders();
    headers.add("Content-Type", MediaType.APPLICATION_FORM_URLENCODED.toString());
    headers.add("Accept", MediaType.APPLICATION_JSON.toString()); //Optional in case server sends back JSON data

    MultiValueMap<String, String> requestBody = new LinkedMultiValueMap<String, String>();
    requestBody.add("name", "feature");
    requestBody.add("color", "#5843AD");

    HttpEntity formEntity = new HttpEntity<MultiValueMap<String, String>>(requestBody, headers);

    ResponseEntity<LabelCreationResponse> response = 
                    restTemplate.exchange("https://example.com/api/request", HttpMethod.POST, 
                                          formEntity, LabelCreationResponse.class);

回答by Nikolay Rusev

I think the problem is that when you try to send data to server didn't set the content type header which should be one of the two: "application/json" or "application/x-www-form-urlencoded" . In your case is: "application/x-www-form-urlencoded" based on your sample params (name and color). This header means "what type of data my client sends to server".

我认为问题在于,当您尝试将数据发送到服务器时,没有设置内容类型标头,它应该是以下两者之一: "application/json" 或 "application/x-www-form-urlencoded" 。在您的情况下是:基于您的示例参数(名称和颜色)的“application/x-www-form-urlencoded”。此标头表示“我的客户端发送到服务器的数据类型”。

RestTemplate restTemplate = new RestTemplate();

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
headers.add("PRIVATE-TOKEN", "xyz");

MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
map.add("name","feature");
map.add("color","#5843AD");

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

ResponseEntity<LabelCreationResponse> response =
    restTemplate.exchange("https://foo/api/v3/projects/1/labels",
                          HttpMethod.POST,
                          entity,
                          LabelCreationResponse.class);