External API with Scriptrunner

Hi, I have a usecase if a Jira Service Management (Server) ticket closed, a SMS will be sent to reporter. Followed Using external APIs in SR - Adaptavist Library this tutorial to utilize Scriptrunner to hit the SMS API but errors appeared after.

import groovy.json.JsonOutput
import groovyx.net.http.ContentType
import groovyx.net.http.HttpResponseDecorator
import groovyx.net.http.RESTClient

final externalUrl = "https://...........................com/"

def body = JsonOutput.toJson(
    			["to": "628138xxxxxxx",
                    "cc": "",
                    "bcc": "",
                    "message": "This is sample",
                    "subject": "",
                    "type": "0001"

def postResponse = post(externalUrl, "/int/notification/1.0.0/send", body)
if (getResponse) {
        def responsePostMap = postResponse as Map
    	responsePostMap.each {}
    assert responseStatus == 200

def post (def hostUrl, def endpointAndQuery, def bodyJson)
  def client = new RESTClient(hostUrl)
        'Accept'       	: ContentType.JSON,
        'x-api-key'		: 'token '

        path: endpointAndQuery,
        contentType: ContentType.JSON,
        body: bodyJson

please help me understand why

groovyx.net.http.ResponseParseException: status code: 200, reason phrase: OK
at groovyx.net.http.HTTPBuilder$1.handleResponse(HTTPBuilder.java:495)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:223)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:165)
at groovyx.net.http.HTTPBuilder.doRequest(HTTPBuilder.java:515)
at groovyx.net.http.RESTClient.post(RESTClient.java:141)
at groovyx.net.http.RESTClient$post.call(Unknown Source)
at Script175.post(Script175.groovy:35)
at Script175.run(Script175.groovy:17)
Caused by: groovy.json.JsonException: Unable to determine the current character, it is not a string, number, array, or object
The current character read is ‘�’ with an int value of 65533
Unable to determine the current character, it is not a string, number, array, or object

Hi Rika,

It looks like your request is successfully processed (response status code is 200 which means OK).

You set ‘Accept’ header to ‘ContentType.JSON’ which defines to handle response body as JSON. But parsing or response fails because � is not valid JSON character.

I would change this header value to “text/plain” to examine response body and after that determine final value of accept header.

Hopefully, it will help you.

Kind Regards
Josef Pavelec,
Morosystems s.r.o., Czech Republic