我需要从我的 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 = …Run Code Online (Sandbox Code Playgroud)