2

I have problem with assigning curl as variable and assign curl's output to variable:

#get results url, format json
URL=$(curl https://api.apifier.com/xy)
#jq is a cli json interpreter
#resultUrl contains the final URL which we want download
OK= "$URL" | jq '.resultsUrl'
#api probably is running
sleep 5
curl "$OK"

Maybe it is trivial, but I don't know where is the problem.

2
  • Change OK= "$URL" | jq '.resultsUrl' to OK=$(echo $URL | jq '.resultsUrl'). You should probably check that OK isn't null before you attempt to curl it. Commented Feb 25, 2016 at 23:01
  • How can you know the correct syntax to set URL, but not use that same syntax to set OK? Commented Feb 25, 2016 at 23:11

2 Answers 2

3

My guess is:

jq '.resultsUrl'

outputs the field resultsUrl with quotes, so curl does not process it correctly. Furthermore, $URL | ... does not work, you would have to use echo or curl directly.

Try

OK=$(curl -s https://api.apifier.com/v1/xHbBnrZ9rxF4CdKjo/crawlers/Example_Alcatraz_Cruises/execute?token=nJ9ohCHZPaJRFEb7nFqtzm76u | jq -r '.resultsUrl')
curl -s "$OK"

which results for me in

[{   "id": 2,   "url": "https://www.alcatrazcruises.com/SearchEventDaySpan.aspx?date=02-25-2016&selected=", "loadedUrl": "https://www.alcatrazcruises.com/SearchEventDaySpan.aspx?date=02-25-2016&selected=", "requestedAt": "2016-02-25T23:24:52.611Z",   "loadingStartedAt": "2016-02-25T23:24:54.663Z",   "loadingFinishedAt": "2016-02-25T23:24:55.642Z",   "loadErrorCode": null,   "pageFunctionStartedAt": "2016-02-25T23:24:55.839Z",   "pageFunctionFinishedAt": "2016-02-25T23:24:55.841Z",   "uniqueKey": "https://www.alcatrazcruises.com/SearchEventDaySpan.aspx?date=02-25-2016&selected=", "type": "UserEnqueued", ...

This should be what you expect.

However, sometimes the first API call yields an error:

{
  "type": "ALREADY_RUNNING",
  "message": "The act is already running and concurrent execution is not allowed"
}

so resultsURL will be null, you will have to handle this error case.

Sign up to request clarification or add additional context in comments.

7 Comments

You are right, I forgot to mention that resultUrl contains quotes. It is still not workin, the current result is: []. If it helps I can post here a sample link.
Yes, do that please ;)
api.myjson.com/bins/4g7q7 - hmm with this json it's working. I think maybe that's the problem, that $OK variable contains URL with parameters?
@Adrian showed you my input and output, what is different on your side?
I do not see any parameters in the resultUrl: https://api.apifier.com/v1/execs/yxcxycxycycx/results
|
1

Your line

OK= "$URL" | jq '.resultsURL'

sets the environment variable OK to an empty string, then tries to execute "$URL" as a command and pipe its output to jq. If you want to setOK to the result of a command, you have to use $OK=(...), just like you did when setting URL. The correct syntax is:

OK=$(echo "$URL" | jq '.resultsURL')

And to remove the quotes from the output of .jq, you can do:

OK=$(echo "$URL" | jq '.resultsURL' | tr -d '"')

1 Comment

For whomever finds their way to this cranny of the internet, in Jun 2020, the jq -r option removes quotes from strings, obviating the need for tr -d '"'.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.