REST with trusted headers in Jira

Create a trusted request with a factory in Jira, add an authentication header with a user of your choice without adding any passwords to the groovy script.

We do this with the absolutely great and fantastic add-on myGroovy. It is free, it is open source, it works.

Post Function

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.sal.api.net.TrustedRequest
import com.atlassian.sal.api.net.Request.MethodType
import com.atlassian.sal.api.net.TrustedRequestFactory
 
import org.apache.log4j.Level
import org.apache.log4j.Logger
 
def myLog = Logger.getLogger("logtest")
myLog.setLevel(Level.ALL)
 
def trustedFactory = ComponentAccessor.getOSGiComponentInstanceOfType(TrustedRequestFactory) as TrustedRequestFactory
def request = trustedFactory.createTrustedRequest(MethodType.GET, "https://www.example.com/jira/rest/my-groovy/latest/custom/logtest")
request.addTrustedTokenAuthentication("www.example.com")
def response = request.execute()
 
myLog.info("response: "+response.toString())
true // well, add any response data you want instead

REST Endpoint

We call our own REST endpoint, also created with myGroovy.

Screenshot REST Endpoint
import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Level
import org.apache.log4j.Logger
 
def myLog = Logger.getLogger("logtest")
myLog.setLevel(Level.ALL)
 
myLog.info("Anything went well if you see me in <jira_home>/log/atlassian-jira.log")