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

回答

相關問題