[英]Getting "400 This page expects a form submission" when making a rest call to trigger a Jenkins Job
I need to trigger a Jenkins Job from my Java code.The Jenkins API expects a application/x-www-form-urlencoded Content-Type and I am able to trigger the job (using Basic AUTH) from Postman Rest Client.However When I try to the same from my java code,I get this exception- HttpClientErrorException: 400 This page expects a form submission:我需要从我的 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)
I am using RestTemplate to call the api.I have set the Content Type header:我正在使用 RestTemplate 调用 api.I 设置了 Content Type 标头:
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
and set the form parameters in a map并在地图中设置表单参数
MultiValueMap<String, String> map = new
LinkedMultiValueMap<String, String>();
map.add("name", "value");
map.add("field2,"value");
map.add("json","a json string");
and the request as:和请求为:
HttpEntity<MultiValueMap<String, String>> request = new
HttpEntity<MultiValueMap<String, String>>(map, headers);
finally making the call as :最后拨打电话:
ResponseEntity<String> response =
restTemplate.exchange(url,HttpMethod.POST, request, String.class);
One thing to note is the expected response is text/html which I want to store in a string and parse to get some values.需要注意的一件事是预期的响应是 text/html,我想将其存储在字符串中并解析以获取一些值。
I had this problem too.我也有这个问题。 Finally, I found that if your job has parameters and you call
/build
, this causes 400
bad request error.最后,我发现如果你的作业有参数并且你调用
/build
,这会导致400
bad request 错误。 You must call /buildWithParameters
even if all the parameters have default values.即使所有参数都有默认值,您也必须调用
/buildWithParameters
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.