0

I have a shell script that will call a REST API and gets the JobID from the response. Using the JobId from the first rest API, I am calling another REST API using cURL. But the issue is the file is not getting downloaded.

I am using basic grep, shell, and cURL.

I am open to use any other alternatives, but not too complex.

I am new to shell scripting. Just doing for a use case that we want to achieve.

Here is my shell script (For security reasons I have changed few things)

#!/bin/bash

echo "Extract jobId from curl request"

RESPONSE=`curl -s --request POST "https://example.com/access/2/abc/jobs/DataDomainExports" -H "accept: application/json" -H "Content-Type: application/json" -d "{ \"association\": [ \"ACCEPTED\" ], \"objectId\": \"DataDomain://FirstName\"}" -u "username:password"`
TEMPJOBID=`echo $RESPONSE | grep -o "DataDomainExports_[A-Za-z0-9]*"` #Using this because the above $RESPONSE is in single line and has the same JobID multiple times.
JOBID=`echo $TEMPJOBID | grep -o -m 1 "^DataDomainExports_[A-Za-z0-9]*"`
echo "The Job ID is:"
echo $JOBID

curl -v -X GET "https://example.com/access/2/abc/jobs/DataDomainExports/"${JOBID}"/result" -u "username:password" -H "accept: application/excel" -o output.csv

echo "export completed"

When I run the above shell script it generates the JobID but doesn't save the file. If I download from chrome it gets downloaded without any issue. I also tried running the scripts individually through the terminal and it works fine and file gets downloaded. Seems like something is wrong.

James Z
  • 12,209
  • 10
  • 24
  • 44
  • Using backticks doesn't quite do what you want when posting the question, and using backticks in general is fairly dated. Instead of backticks, you should probably use `$()`. eg, `RESPONSE=$( curl ... )`. It is easier for posting (since there are no backticks to be misinterpreted as markdown) and it is easier to maintain the code. `$()` has been available as a replacement for backticks for over 2 decades. – William Pursell Apr 28 '23 at 13:55
  • You are using quotes incorrectly. Instead of `curl -v -X GET "https://example.com/access/2/abc/jobs/DataDomainExports/"${JOBID}"/result"`, you want to ensure that the `${JOBID}` is in quotes. You can do either `curl -v -X GET "https://example.com/access/2/abc/jobs/DataDomainExports/${JOBID}/result"` or `curl -v -X GET https://example.com/access/2/abc/jobs/DataDomainExports/"${JOBID}"/result`. The former is almost certainly preferred. The general rule is that bare strings do not need to be quoted, but variables do. It never hurts to quote bare strings though, and doing so is often wise. – William Pursell Apr 28 '23 at 14:11
  • [tag:sh] - "For shell scripts with syntax or other errors, please check them at http://shellcheck.net before posting them here.". See also [correct-bash-and-shell-script-variable-capitalization](https://stackoverflow.com/questions/673055/correct-bash-and-shell-script-variable-capitalization). – Ed Morton May 03 '23 at 11:10

0 Answers0