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

1

14

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?

Zelma answered 21/6, 2016 at 13:8 Comment(2)
Have you tried using curl for that? It has the benefit of being easily testable in the shell.Walrath
I did not try that...I need to pull in and manipulate the response object, and I read somewhere that's harder to do with curl so I did not think that was a viable option.Zelma
M
4

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

Monseigneur answered 23/6, 2016 at 20:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.