Forums

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

Put Call to update external field via Script Runner

Tobias
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.
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

0 answers

Suggest an answer

Log in or Sign up to answer