sou*_*ami 18 java spring rest-client resttemplate jenkins
我需要从我的 Java 代码触发一个 Jenkins 作业。Jenkins API 需要一个 application/x-www-form-urlencoded Content-Type 并且我能够从 Postman Rest Client 触发这个作业(使用 Basic AUTH)。但是当我尝试从我的 java 代码中进行相同的操作,我收到此异常 - HttpClientErrorException: 400 此页面需要提交表单:
Exception while triggerring jenkins {} 400 This page expects a form submission
org.springframework.web.client.HttpClientErrorException: 400 This page expects a form submission
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:108)
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:708)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:661)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:621)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:539)
Run Code Online (Sandbox Code Playgroud)
我正在使用 RestTemplate 调用 api.I 设置了 Content Type 标头:
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
Run Code Online (Sandbox Code Playgroud)
并在地图中设置表单参数
MultiValueMap<String, String> map = new
LinkedMultiValueMap<String, String>();
map.add("name", "value");
map.add("field2,"value");
map.add("json","a json string");
Run Code Online (Sandbox Code Playgroud)
和请求为:
HttpEntity<MultiValueMap<String, String>> request = new
HttpEntity<MultiValueMap<String, String>>(map, headers);
Run Code Online (Sandbox Code Playgroud)
最后拨打电话:
ResponseEntity<String> response =
restTemplate.exchange(url,HttpMethod.POST, request, String.class);
Run Code Online (Sandbox Code Playgroud)
需要注意的一件事是预期的响应是 text/html,我想将其存储在字符串中并解析以获取一些值。
Toh*_*had 13
我也有这个问题。最后,我发现如果你的工作有参数并且你调用/build,这会导致400错误的请求错误。/buildWithParameters即使所有参数都有默认值,您也必须调用。
| 归档时间: |
|
| 查看次数: |
3820 次 |
| 最近记录: |