Scriptrunner cURL error, ignore -H option

Jordan Berry January 9, 2020

It appears that my script is missing my -H option when I am trying to get a response. It is complaining that it is a syntax issue, but it doesn't appear to be. Anyone seen this issue before? I have taken out any identifying info about the API. I have tested this in a terminal, and it returns data correctly

Code:

import org.apache.log4j.Level
import org.apache.log4j.Logger

def log = Logger.getLogger("com.acme.workflows")
log.setLevel(Level.DEBUG)

def headers = "Bearer 12345678-1234-1234-1234-123456789120"
//def response = "curl -H \"Authorization: Bearer 12345678-1234-1234-1234-123456789120\" https://url.com/api/path/end"
def response = 'curl -H \'Authorization: Bearer 12345678-1234-1234-1234-123456789120\' https://url.com/api/path/end'
def proc = response.execute();
proc.waitFor();

log.debug(proc.exitValue())
log.debug(proc.err.text)
log.debug(proc.in.text)

 

I have also tried without the escapes (\') and with double quotes. It seems like each combination is still throwing errors

 

Errors:

curl: (6) Could not resolve host: Bearer; Name or service not known

curl: (6) Could not '; Name or service not known

Response:

{"timestamp":1578586566943,"status":401,"error":"Unauthorized","message":"Full authentication is required to access this resource","path":"/api_data/all_data"}

1 answer

0 votes
Randy
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.
January 9, 2020

What are you trying to access from the API?  Is there a reason you can't leverage the com.atlassian.jira libraries?

Jordan Berry January 9, 2020

I was not aware there were libraries that could handle those calls

I am trying to access a remote DB that stores details information regarding to billing. The return should be a json dump of information.

Randy
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.
January 9, 2020

Ah gotcha.  Didnt realize you were calling out to an external endpoint.  In that case a few things to note:

1. The error is saying "could not resolve host" which indicates that it's not catching the parameters as being part of the header arg but instead as the target address.  This indicates that the value isnt being recognized as a header value...the quotes arent getting passed through.

Try:

def response = "curl -H 'Authorization: Bearer 12345678-1234-1234-1234-123456789120' https://url.com/api/path/end"

2. Use HttpClient instead for all it's benefits

3. You may have compromised your access token.  get a new one before you push this to a production environment ;)

curl: (6) Could not resolve host: d4483b04-e671-4042-94fd-db2027f3a5cd'; Name or service not known
Jordan Berry January 9, 2020

Yikes you are right. It has to be regenerated anyways. I will try your suggestions in the morning, but do you have an example of the httpclient?

Suggest an answer

Log in or Sign up to answer