performing HTTP Post from scriptrunner

Alex van Rijs October 4, 2017

I created a custom field called 'E-mail Address' . After transititioning an issue to a certain state I want to send an HTTP Post request to a certain url if an email is listed in that issue.

Currently I am checking the empty email condition with:

ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Contact E-mail") != ""

 

Does anybody have an Idea how I can send the value of the email value to an url: www.exampleurl.com with argument email=issueemail

 

Thanks!

1 answer

0 votes
Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 4, 2017

Here's example of working code -

https://community.atlassian.com/t5/Product-Apps-questions/help-with-REST-using-groovy/qaq-p/17649

 

To check if field is null or present you can do that with "if" statement in groovy

def customFieldManager = ComponentAccessor.getCustomFieldManager();
def email = customFieldManager.getCustomFieldObjectByName('Email');
// this is the check
if(issue.getCustomFieldValue(email)) {
//code here for REST POST call
} else {
//do nothing
}
Alex van Rijs October 5, 2017

Hi Tarun,

I combined the suggestion you posted with a default REST Post groovy example. I am using the following script, but the importing of the classes/dependencies seem to fail.

 

import com.atlassian.jira.ComponentAccessor;
import groovyx.net.http.HTTPBuilder;
import static groovyx.net.http.ContentType.URLENC;

def customFieldManager = ComponentAccessor.getCustomFieldManager();
def email = customFieldManager.getCustomFieldObjectByName('Email Adress');

// this is the check
if(issue.getCustomFieldValue(email))
{
    def http = new HTTPBuilder( 'http://www.example.com/Scripts/test.php' );
    def postBody = [username: 'bob']; // will be url-encoded
 
    http.post( path: '/', body: postBody, requestContentType: URLENC )
    {
        resp -> println "POST Success: ${resp.statusLine}";
        assert resp.statusLine.statusCode == 201;
    }
}
else
{
    //do nothing
}

The debug/output/error console lists the following errors after running the code:

Script2.groovy: 1: unable to resolve class com.atlassian.jira.ComponentAccessor
@ line 1, column 1.
   import com.atlassian.jira.ComponentAccessor;
   ^

Script2.groovy: 2: unable to resolve class groovyx.net.http.HTTPBuilder
@ line 2, column 1.
   import groovyx.net.http.HTTPBuilder;
   ^

Script2.groovy: 3: unable to resolve class groovyx.net.http.ContentType
@ line 3, column 1.
   import static groovyx.net.http.ContentType.URLENC;
   ^

3 errors



settings.JPG

 Do you have any idea what is going on here?

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events