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
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.