API Put call via ScriptRunner to update external field

Tobias November 29, 2021

Hello all,
we are currently populating an external system with the Jira ID via a put call. We need this to be able to send updates from this system to the respective Jira issue.

Thus, when creating the issue via workflow, we want to trigger a script runner script which will then perform the put post. However, we are currently still getting an error message, the body is intentionally empty for now for testing purposes. The endpoint differs to a field value which is filled by the creation, this is happening via post call from this external system. 

I suspect the syntax of my script does not fit.

package utils
import groovy.json.JsonOutput

import groovyx.net.http.ContentType

import groovyx.net.http.HttpResponseDecorator

import groovyx.net.http.RESTClient

import com.atlassian.jira.issue.Issue

import com.atlassian.jira.component.ComponentAccessor

public class UpdateFieldValuePut{

public static String postResponse(Issue issue)

    {

def hostUrl = "https://test-api.com/"

def customField = ComponentAccessor.customFieldManager.getCustomFieldObject("customfield_12727")

def strErgebnis = issue.getCustomFieldValue(customField).toString()

def endpointAndQuery = "https://test-api.com/test/" +strErgebnis+".json"

        // Turn the data will be sent into JSON, the HTTP requests specify in the header to accept application/JSON

        // If the API accepts different types then you can change this and format the data accordingly

def bodyJson = JsonOutput.toJson([data"<YOUR DATA>"

        ])

    def client = new RESTClient(hostUrl)

    client.setHeaders([

        'Accept'       : ContentType.JSON,

            // If authentication mechanism of the API is Basic Authentication, 'Authorization' header with username and password encoded as Base 64 can be specified here

        'Authorization': "Basic ${'APIToken:x'.bytes.encodeBase64()}"

        ])

    client.handler.success = { HttpResponseDecorator response, json ->

        json

        }

    client.handler.failure = { HttpResponseDecorator response ->

        // Failure can be handled here

        log.error response.entity.content.text

        [

                :

            ]

        }

    client.put(

        path: endpointAndQuery,

        contentTypeContentType.JSON,

        bodybodyJson

    )

    }
}

Best regards,

Tobias

1 answer

1 vote
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 7, 2021

Hi @Tobias

Could you please clarify, when you mentioned External System, i.e. are you referring to an external REST Service?

If so, you could try to use this sample code provided in the Adaptavist Library.   

I hope this helps to answer your question. :)

 

Thank you and Kind Regards,

Ram

Suggest an answer

Log in or Sign up to answer