How to call REST from jenkins workflow

前端 未结 5 1925
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-24 06:23

I wonder how to call REST API from a (groovy) Jenkins workflow script. I can execute \"sh \'curl -X POST ...\'\" - it works, but building the request as a curl command is cu

5条回答
  •  Happy的楠姐
    2020-12-24 07:05

    Have you tried Groovy's HTTPBuilder Class? For example:

    @Grapes(
        @Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7.1')
    )
    
    import groovyx.net.http.HTTPBuilder
    import static groovyx.net.http.ContentType.*
    import static groovyx.net.http.Method.*
    
    def http = new HTTPBuilder("http://api.geonames.org/citiesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&lang=de&username=demo")
    
    http.request(POST, JSON ) { req ->
        body = []
        response.success = { resp, reader ->
            println "$resp.statusLine   Respond rec"
    
        }
    }
    

提交回复
热议问题