Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

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

André Diogo Graça
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
April 28, 2021

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
PD Sheehan
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.
May 4, 2021

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