1

I am giving the user the possibility to answers some questions so that I can perform the according cURL requests and create what is needed accordingly.

In this way:

~functions.sh

echo "Question number 1?"
read question1
echo "Question number 2?"
read question2

The script is organised in a couple of files:

  • functions.sh, where I store all the functions
  • flow.sh, where the questions are asked and the previous functions are called in order
  • credentials.sh, where I store the api key credentials, api url etc.
  • env.sh, where I am hoping to store the env configuration that needs to be dynamically generated...

Unfortunately I am pretty much new to that as I come from a Frontend Background so I Am not aware or can't figure out/find on here, how to achieve the following:

I would need to send a cURL request based on one of the users answers, and that's easily done just by calling ie {$question1} inside one of the functions of functions.sh that is later called in the main flow.sh after putting the file as a source...

Unfortunately, when it comes to populating a multiline environment configuration in a separate file to be later be sent as a json in the cURL it gets out of my comfort zone...

Id need so send something along the lines of

curl -X POST "https//api.url"
-H "headers"
--data '{"content":"'{$env}'"}'

Where $env is the env.sh file, json valid, and populated with the variables read before from user input accordingly...

To solve the multi line json problem maybe I could use the following:

$( awk '{printf "%s\\n", $0}' ./env.sh )

Still I am clueless on how to populate such file form user read and echo it inside that json part in the cURL...

2 Answers 2

2

You could use awk to directly save correct JSON data to env.sh (partly taken from Dr.K's answer to this other question):

echo "Question number 1?"
read question1
echo "Question number 2?"
read question2
echo "Question number 3?"
read question3

awk ' BEGIN { print "[" ; }                     \
      { print  " {"                             \
        print  "   \"Question1\" : \""     $question1  "\","   \
        print  "   \"Question2\" : \""   $question2  "\","   \
        print  "   \"Question3\" : \""  $question3  "\""   \
        print  " }"                             \
      }                                         \
      END { print "]" } '  env.sh     

And then this should work correctly as JSON:

curl -X POST "https//api.url"
-H "headers"
--data '{"content":"{$env}"}'

and the data payload should be like this:

{
  content: [
    {
      "Question1" : "User answer 1",
      "Question2" : "User answer 2",
      "Question3" : "User answer 3"
    }
  ]
}

Is this what you had in mind?

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

1 Comment

I would like to keep the env.sh in a separate file for the user/me to just copy and paste their .env configuration and the script to take care of everything by itself instead of "hardcoding" it in the questions
0

For anyone who ends up here what I did was:

Creating a separate file where I set a variable and I cat the EOF inside of that.

env=$( cat <<EOF
multi
line
content
goes
here
EOF
)

export env

Then I generate a valid json through jq

generate_json() {
  newenv="$( jq -nc --arg str "$env" '{"content": $str}' )"
}

and finally I use $newenv in the curl straight away:

example_curl() {
  curl -X PUT "https://apiendpoint.co.uk" \
  -H "Authorization: Bearer 123123" \
  --data "${newenv}"

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.