0

I am trying to use curl with a URL containing two dates. How can I use the values of two variables containing date/time stamps instead of hard coded dates in the URL? I want to use a range from 50 minutes ago to the current time.

Here is my code with hard coded date/time values:

#!/bin/bash

currentTime=$(date +"%R")
currentdate=$(date +'%m/%d/%Y')
oldtime=$(date +%R -d "50 min ago")

echo "Current time : $currentTime"
echo "Current Date : $currentdate"
echo "Old time : $oldtime"

Response=$(curl -X GET -H "Authorization: Basic Token=" "http://targetserver.companyname.com:8080/v1/organizations/companyname/environments/environmentname/stats/apis?select=sum(is_error)&timeRange=01/24/2017%2002:00~01/25/2017%2006:00")

I tried replacing the hard coded dates as follows, but it didn't work:

Response=$(curl -X GET -H "Authorization: Basic Token=" "http://targetserver.company.com:8080/v1/organizations/company/environments/environmentname/stats/apis?select=sum(is_error)&timeRange="$currentdate" "$currentTime"~"$currentdate" "$oldtime"")
6
  • 5
    Could you show at least one way you tried? Also, what does this have to do with r? Commented Jan 26, 2017 at 14:18
  • 1
    You know how to use a variable in a string - you have used in echo statements. You can use the same mechanism in the curl statement. Commented Jan 26, 2017 at 14:41
  • Hi chepner ... please see below the way I was trying ... Response=$(curl -X GET -H "Authorization: Basic Token=" "targetserver.companyname.com:8080/v1/organizations/companyname/…) echo -e "The response is :\n $Response" value=$(Response|jsawk 'return this.values') echo "the value is : $value" The error is "command not found" I am using cygwin, and tried downloading different js package but still not working. Commented Jan 26, 2017 at 15:24
  • Response=$(curl -X GET -H "Authorization: Basic Token=" "targetserver.company.com:8080/v1/organizations/company/…) echo -e "the response is: \n $Response" Tried but did not work. Commented Jan 26, 2017 at 19:55
  • Hi Chepner please see the below command. It's not working though. Response=$(curl -X GET -H "Authorization: Basic Token=" "targetserver.company.com:8080/v1/organizations/company/…) echo -e "the response is: \n $Response" Commented Jan 26, 2017 at 20:47

1 Answer 1

2
Response=$(curl -X GET -H "Authorization: Basic Token=" "http://targetserver.company.com:8080/v1/organizations/company/environments/environmentname/stats/apis?select=sum(is_error)&timeRange=${currentdate}%20${currentTime}~${currentdate}%20${oldtime}")

I made a working example, here:

#!/bin/bash
example="Just_A_Test_String"
response=$(curl -X GET "http://5.135.224.191/test_curl.php?var1=${example}")
echo $response
Sign up to request clarification or add additional context in comments.

2 Comments

echo "$response" would be a bit less buggy with the double quotes; see BashPitfalls #14.
Completely agree @CharlesDuffy - I normally do that, however slipped my mind at the time.

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.