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

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

I cant do Post Web Requests(rest api) using Scriptrunner

Greetings 

 

I'm using the following code

 

def baseUrl = new URL(‘http://posturl.wtv’)
def connection = baseUrl.openConnection()
def jsonString = ['{"assetstatus": 3,"cinum": "LIS1SBD505","startdatetime": "20210413T16:47:00+00:00","finishdatetime": "20210413T16:49:00+00:00","rootcause": "SSBD.Passageiro.Utilização incorreta","summary": "Scriptrunner Test","ticketca": "TICKET-333333","ticketstatus": "Em Progresso"}']
connection.setRequestProperty(‘authorizationheader’,’base64code’)
connection.with {
doOutput = true
requestMethod = "POST"
outputStream.withWriter { writer ->
writer << jsonObj
}
log.warn(connection.content.text)
log.warn(connection.contentType)
connection.headerFields.each { println "> ${it}"}
}

 

I always get code 200 and if i do a get request i dont get the object asa output.

 

Can someone help?

 

Best Regards 

 

1 answer

0 votes

Scriptrunner comes pre-loaded with some libraries that make doing rest request easier than what you're trying.

You can see their examples here: https://library.adaptavist.com/entity/using-external-apis-in-sr?_ga=2.145965245.826574474.1620181182-1635025387.1620181182

I haven't played with the RestClient class myself, I learned to use the HttpBuilder class instead and have stuck with it... here is an example re-using some of your example:

 

import groovyx.net.http.ContentType
import groovyx.net.http.HTTPBuilder
import groovyx.net.http.Method
import groovyx.net.http.HttpResponseDecorator

def payload = [
[
assetstatus: 3,
cinum: "LIS1SBD505",
startdatetime: "20210413T16:47:00+00:00",
finishdatetime: "20210413T16:49:00+00:00",
rootcause: "SSBD.Passageiro.Utilização incorreta",
summary: "Scriptrunner Test",
ticketca: "TICKET-333333",
ticketstatus: "Em Progresso"
]
]
def httpBuilder = new HTTPBuilder('your url')
def parsedResponseObject = httpBuilder.request(Method.POST, ContentType.JSON) {
body = payload
headers.authorizationheader = 'base64code'
response.success = {HttpResponseDecorator resp, json ->
log.info resp.statusLine.statusCode
resp.headers.each{log.info("$it.name = $it.value")}
return json
}
response.failure = {HttpResponseDecorator resp, text ->
log.error "Error: $text"
return [error: text, response:[status:resp.statusLine.statusCode]]
}
}

 That might help you get to the bottom of your error.

Suggest an answer

Log in or Sign up to answer
TAGS

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you