Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Put Call to update external field via Script Runner

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
TAGS
Community showcase
Published in Jira Software

👋 Looking for 15-20 volunteers to test Atlassian training content

Hi everyone! Are you interested in beta testing Atlassian University’s newest (unreleased!) training course? We’re looking for 15-20 volunteers to test our newest training course, Basic reporting...

413 views 19 22
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you