Trying to allow dynamic bash var for the prompt but escape char not working any ideas?
output=$(curl https://api.openai.com/v1/images/generations \
-H 'Content-Type: application/json' \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"prompt": "$prompt",
"n": 1,
"size": "1024x1024"
}'
Tried escaping char but get error please help me allow bash vars in curl request.
CodePudding user response:
The single quotes around the '-d' (--data) values are preventing the expansion of the variable.
Note that we are escaping the other double quotes in the json so they show up when the command is run. I assume you wanted them there, hence the single quotes.
You can do something along these lines (I like using the long version of options to show what the arguments are doing in a script).
$ prompt="A cute baby sea otter"
$ OPENAI_API_KEY=S0MeCo0lk3y
$ output=$(curl https://api.openai.com/v1/images/generations \
--header "Content-Type:application/json" \
--header "Authorization: Bearer ${OPENAI_API_KEY}" \
--data "{ \"prompt\":\"${prompt}\",\"n\": 1,\"size\": \"1024x1024\"}")
That sends the following:
curl https://api.openai.com/v1/images/generations --header Content-Type:application/json --header 'Authorization: Bearer S0MeCo0lk3y' --data '{ "prompt":"A cute baby sea otter","n": 1,"size": "1024x1024"}'
Checkout https://mywiki.wooledge.org/BashFAQ/050 as it might help with what you are trying to do.