简体   繁体   English

在进行休息调用以触发 Jenkins 作业时出现“400 此页面需要提交表单”

[英]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.

相关问题 Jenkins 工作的外部触发器 - External Trigger for Jenkins job 使用 Springboot 和 feign 客户端进行 rest 调用时获取选择性字段作为响应 - Getting selective fields in response when making a rest call using Springboot and feign client Spring mvc HTTP 状态 400 表单提交 - Spring mvc HTTP Status 400 form submission 调用AJAX方法时获得400(错误请求) - Getting 400(Bad Request) when giving a call to AJAX method 无法通过java代码触发jenkins作业 - Unable to trigger jenkins job through java code ResourceAccessException with SocketException: Connection reset, 当通过代理进行 REST 调用时 - ResourceAccessException with SocketException: Connection reset, when making a REST Call through a proxy 我在Internet Explorer中仅收到400错误的ReST呼叫请求 - I am getting 400 Bad Request for ReST call only in internet explorer 建立SNAPSHOT依赖项时如何触发具有多个分支的jenkins maven作业? - How to trigger jenkins maven job with multiple branches when a SNAPSHOT dependency is built? 为什么在REST调用activiti-rest时出现404错误? - Why am I getting 404 error making REST call to activiti-rest? 由于未连接表单,因此取消了提交表单的操作 - Getting Form submission canceled because the form is not connected
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM