使用 Jenkins 的 http-request 插件 v1.8.10(我运行的是 1.643),现在支持在请求中 POSTing 正文 - 所以这thread https://stackoverflow.com/questions/36082421/where-body-is-put-in-post-with-http-request-plugin-in-jenkins不适用。我想知道如何在 Pipeline (v2.1) Groovy 脚本中使用此功能?代码片段生成器不包含这个新字段,因此我没有可以构建的示例。
我尝试了各种方法将 JSON 数据放入请求正文中,但我的 Tomcat 服务器总是返回 http 400 状态代码:The request sent by the client was syntactically incorrect.
我尝试过的事情:
def toJson = {
input ->
groovy.json.JsonOutput.toJson(input)
}
def body = [
displayName: [
text: "smoke test"],
description: [
text: "for smoke testing"],
genusTypeId: "type"
]
response = httpRequest consoleLogResponseBody: true, contentType: 'APPLICATION_JSON', httpMode: 'POST', requestBody: toJson(body), url: "https://${host}", validResponseCodes: '200'
def body = [
displayName: [
text: "smoke test"],
description: [
text: "for smoke testing"],
genusTypeId: "type"
]
response = httpRequest consoleLogResponseBody: true, contentType: 'APPLICATION_JSON', httpMode: 'POST', requestBody: body, url: "https://${host}", validResponseCodes: '200'
response = httpRequest consoleLogResponseBody: true, contentType: 'APPLICATION_JSON', httpMode: 'POST', requestBody: "{\"displayName\":{\"text\":"smoke test\"},\"description\":{\"text\":\"for smoke testing\"}, \"genusTypeId\":\"type\"}", url: "https://${host}", validResponseCodes: '200'
response = httpRequest consoleLogResponseBody: true, contentType: 'APPLICATION_JSON', httpMode: 'POST', requestBody: "'{\"displayName\":{\"text\":"smoke test\"},\"description\":{\"text\":\"for smoke testing\"}, \"genusTypeId\":\"type\"'}", url: "https://${host}", validResponseCodes: '200'
扫描http-request
库代码,看来设置这个标志应该有效。我不知道 Pipeline 插件/Jenkins 插件是如何工作的,所以我想知道 Pipeline -> http-request 代码是否解释了这个新参数?有人可以告诉我如何使带有请求主体的 POST 与 Pipeline 一起使用,或者我需要在哪里修改 Pipline 插件代码才能建立连接吗?