如何使用 Jenkins http-request 插件和管道 POST JSON 数据?

How to POST JSON data in body with Jenkins http-request plugin and Pipeline?

使用 Jenkins 的 http-request 插件 v1.8.10(我是 运行 1.643),现在支持在请求中发布正文——所以这个 thread不适用。我想知道如何在管道 (v2.1) Groovy 脚本中使用此功能? Snippet Generator 不包括这个新字段,所以我没有构建示例。

我尝试了各种方法将 JSON 数据放入请求正文中,但我的 Tomcat 服务器总是 returns 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 与管道一起工作,或者我需要在哪里修改 Pipline 插件代码以建立连接?

我认为这是一个错误。我添加了 https://issues.jenkins-ci.org/browse/JENKINS-36203

和公关:https://github.com/jenkinsci/http-request-plugin/pull/15