2

对于Jenkins的http请求插件(我正在运行1.643)的v1.8.10,现在支持在请求中发布主体 - 因此此thread不适用。我想知道如何在Pipeline(v2.1)Groovy脚本中使用这个功能?代码片段生成器不包含这个新字段,所以我没有任何示例来构建。如何使用Jenkins http-request插件和管道将body中的JSON数据POST?

Snippet Generator

我曾尝试过各种方法来获得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代码是否解释了这个新参数?有人可以指示我如何使用请求主体与管道一起工作,或者我需要修改Pipline插件代码以建立连接?

+0

您是否尝试过使用'curl'是什么?它具有在壳体中易于测试的优点。 –

+0

我没有尝试......我需要拉入并操作响应对象,并且我在某处读取卷曲很难做的事情,所以我不认为这是可行的选择。 – user

回答

相关问题