0

I am passing two date values to a script and trying to use those in a CURL POST command as below:

starttime=$1
endtime=$2

for apps in $(cat testapps.txt)
do
    curl -X POST -H "Content-type: application/vnd.appd.cntrl+json;v=1" -d '{"name": "This is a test","timeRange": {"startTimeMillis":"$1","endTimeMillis":"$2"}, "affects": {"type": "APP"}}'

It is giving me 500 internal server error.

If I replace the $1 and $2 value with the date/time as below, it works fine.

curl -X POST -H "Content-type: application/vnd.appd.cntrl+json;v=1" -d '{"name": "This is a test","timeRange": {"startTimeMillis":"2019-05-28T15:00:00-0400","endTimeMillis":"2019-05-28T16:00:00-0400"}, "affects": {"type": "APP"}}'

Am I missing anything?

5
  • 4
    you are using the variables $1 and $2 inside single quotes. see after -d you are using single quotes. In bash you cannot expand the variable when its inside single quote. Try using double quotes. Commented May 28, 2019 at 18:47
  • Add a shebang and then paste your script there: shellcheck.net Commented May 28, 2019 at 18:53
  • Getting this error when I replace single quote with double - curl: (6) Could not resolve host: is; Unknown error curl: (6) Could not resolve host: a; Unknown error curl: (3) [globbing] unmatched close brace/bracket at pos 120 Commented May 28, 2019 at 19:09
  • you need to properly escape it before using it, as the input json already contains double quotes. Commented May 28, 2019 at 19:25
  • Thank you so much. It works. Commented May 28, 2019 at 19:37

1 Answer 1

1

First, Dont Read Lines With For.

while read apps
do args=( 
    -X POST
    -H "Content-type: application/vnd.appd.cntrl+json;v=1"
    -d '{ "name":"This is a test",
          "timeRange": { 
              "startTimeMillis": "'"$1"'",
              "endTimeMillis": "'"$2"'" }, 
          "affects": {"type": "APP"}}'
   ) 
   # don't you need a URL here?
   curl "${args[@]}" "$apps" # added $apps, assuming that was missing...
done < testapps.txt
Sign up to request clarification or add additional context in comments.

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.