0

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.

Barmar
  • 741,623
  • 53
  • 500
  • 612
Jay Mee
  • 55
  • 1
  • 11
  • `$prompt` is inside single quotes. Variables are only expanded inside double quotes. – Barmar Nov 05 '22 at 20:11
  • And you seem to understand that, since you used double quotes for the `Authorization` header. Why would it be different for `-d`? – Barmar Nov 05 '22 at 20:12
  • See: [Difference between single and double quotes in bash](http://stackoverflow.com/q/6697753/3776858) – Cyrus Nov 05 '22 at 20:17
  • becuase its json and when i change quotes i get error, when i use esc char and quote it still gives error! – Jay Mee Nov 05 '22 at 20:29
  • please update the question to show what the (complete) `curl` call should look like with your variables plugged in – markp-fuso Nov 05 '22 at 21:18

1 Answers1

1

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.

TheAnalogyGuy
  • 376
  • 2
  • 9
  • Thank you, I have an issue with getting 1 field from a json response but i cant post as this site has prevented me from making anny more posts :/ come to jnet.club and chat to me there if you know json please, php and json is supposed to be easy but isnt working for some reason :/ – Jay Mee Nov 05 '22 at 22:26
  • No problem - thanks for the upvote - I need to get on here and contribute more. For json, check out https://jsonformatter.org/json-parser - great tool to play around with to get to know json. – TheAnalogyGuy Nov 05 '22 at 22:41
  • This is also another great resource https://www.w3schools.com/js/js_json_intro.asp – TheAnalogyGuy Nov 05 '22 at 22:46
  • how do I get a string from a json array via php? that looks like this https://pastebin.com/Laz9uVta – Jay Mee Nov 05 '22 at 23:25
  • Sorry man - I am not a php dude but this answer may help - https://stackoverflow.com/questions/7511821/how-to-convert-json-string-to-array – TheAnalogyGuy Nov 06 '22 at 20:26