Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

With v1.8.10 of the http-request plugin for Jenkins (I'm running 1.643), there is now support for POSTing a body in the request -- so this thread does not apply. I am wondering how to use this functionality in a Pipeline (v2.1) Groovy script? The Snippet Generator does not include this new field, so I have no example to build off of.

Snippet Generator

I have tried various ways to get the JSON data into the request body, but my Tomcat server always returns http 400 status code: The request sent by the client was syntactically incorrect.

Things I have tried:

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'

Scanning the http-request library code, it seems like setting this flag should work. I don't know how Pipeline plugins / Jenkins plugins work, so I wonder if the Pipeline -> http-request code accounts for this new parameter? Can someone point me to how I can make POSTs with request bodies work with the Pipeline, or where I need to modify Pipline plugin code to make the connection?

like image 596
user Avatar asked Jun 21 '16 13:06

user


1 Answers

I think this is a bug. I added https://issues.jenkins-ci.org/browse/JENKINS-36203

and the PR: https://github.com/jenkinsci/http-request-plugin/pull/15

like image 63
Isaac Cohen Avatar answered Sep 20 '22 12:09

Isaac Cohen