Home > OS >  How to use curl to post a file, substituting env variables
How to use curl to post a file, substituting env variables

Time:10-15

How can I use curl and/or bash to post a file, substituting env variables.

Lets say I have test.json like so (which I can edit the format of):

{
  "name": "'"$NAME_VAR"'"
}

how can I post this file via curl, while substituting any env variable?

Example curl of posting a test.json file without substitutions:

curl -X POST -H 'Content-Type: application/json' --data-binary @test.json http://test-server/some/path

CodePudding user response:

This seems like an XY Problem. Even if it isn't the question is underspecified. Does the solution have to support arbitrary var names or are they known apriori? Can the values contain quote marks that need to be properly escaped when interpolated into the JSON text? Why does the JSON text contain this unusual quoting:

"name": "'"$NAME_VAR"'"

If that can be changed to a single double-quote on each side of the env var name, and the env var value doesn't contain quotes, then the envsubst command is the simplest solution.

  • Related