Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,556,318
Community Members
 
Community Events
184
Community Groups

How to POST to a Rest API using Scriptrunner

Hi folks,

I'm trying to post to a rest api using scriptrunner:

import groovyx.net.http.HTTPBuilder
import static groovyx.net.http.ContentType.*
import static groovyx.net.http.Method.*
import org.apache.log4j.Logger
import org.apache.log4j.Level

def log = Logger.getLogger("com.acme.CreateSubtask")
log.setLevel(Level.DEBUG)

def http = new HTTPBuilder('https://serviceportal.pornhub.local:443/')
def body = '{"parameters": [ { "value": { "string": { "value": "testu" } }, "type": "string", "name": "username", "scope": "local" }]}'

try
{
http.request( POST, JSON ) {req ->
uri.path = 'vco/api/workflows/34c1dc35-1d56-40ec-8741-18fb6ed0d59e/executions/'
body = body

response.success = { resp ->
println "POST response status: ${resp.statusLine}"
assert resp.statusLine.statusCode == 201
}
}
}
catch (groovyx.net.http.HttpResponseException ex) {
log.debug ex.printStackTrace()
log.debug ex.toString()
}

 

The call works fine in postman but all i get when i run this through the script console is a groovyx.net.http.HttpResponseException with no details as to what I am doing wrong.  Can anyone point out what I am doing wrong?


thanks

 

1 answer

1 accepted

0 votes
Answer accepted
Ivan Tovbin
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
Oct 24, 2019 • edited

Hi Chris,

First off I'd start with configuring proper handlers for your HttpBuilder. That way you you'll always be able to see what is wrong instead of simply getting an exeption thrown at your face.

I wrote this simple method to create an instance of HttpBuild with all handlers configured:

import groovyx.net.http.HTTPBuilder

HTTPBuilder getHttpBuilder(String url) {
HTTPBuilder http = new HTTPBuilder(url)
http.setHeaders(["X-Atlassian-Token": "no-check"])
http.getHandler().failure = { respObject, body ->
[response: respObject, body: body]
}
http.getHandler().success = { respObject, body ->
[response: respObject, body: body]
}
return http
}

Just don't forget to add Authorization header to it.

Now that we have it out of the way I think I found what's wrong with your code. Your request body is a string while is should actually be a JSON. Here's how you fix it:

 

import groovy.json.JsonOutput

def body = JsonOutput.toJson(["parameters": [ { "value": { "string": { "value": "testu" } }, "type": "string", "name": "username", "scope": "local" }]])

Hope this helps 

Thanks Ivan,

Just what I needed :)

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events