如何使用Jenkins http-request插件和Pipeline在正文中发布JSON数据?

时间:2016-06-21 13:08:46

标签: jenkins groovy jenkins-plugins http-request jenkins-pipeline

使用Jenkins的http-request插件的v1.8.10(我运行1.643),现在支持在请求中POST一个正文 - 所以这个thread不适用。我想知道如何在Pipeline(v2.1)Groovy脚本中使用此功能?片段生成器不包含这个新字段,因此我没有示例来构建。

Snippet Generator

我尝试了各种方法将JSON数据放入请求体,但我的Tomcat服务器始终返回http 400状态代码:premis:object

我尝试过的事情:

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' 库代码,似乎设置此标志应该有效。我不知道Pipeline插件/ Jenkins插件是如何工作的,所以我想知道是否有Pipeline - > http请求代码是否考虑了这个新参数?有人可以指出我如何使用请求主体使用管道进行POST,或者我需要修改Pipline插件代码来进行连接吗?

1 个答案:

答案 0 :(得分:1)