Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass current date to a curl query using shell script?

I am inserting data to a elastic search using CURL, it works fine when i insert with a fixed data. I am trying to Get Current DateTime and assign to a variable and use with the object that i want to insert.

Here is my Script,

while true;
do

echo $i

number=$RANDOM;
let "number %= 9";
let "number = number + 1";
range=10;
for i in {1..18}; do
  r=$RANDOM;
  let "r %= $range";
  number="$number""$r";
done;

curl -XPUT 'http://localhost:9200/nondomain_order/orders/'+$number+'' -d '{
  "CustType": null,
  "tag": "OrderType:Postpaid",
  "GUDeviceID": "0",
  "IsAvailable": false,
  "GUOrderID": "123",
  "OrderID": "3",
  "OrderDate": "2015-01-06T15:23:42.7198285+05:30",
  "GUAccountID": "15010615234251403",
  "CreateUser": "admin",
  "CreateDate": "2015-01-01T15:23:42",
  "CancelledDate": "1899-01-01T00:00:00",
  "CancelledUser": null,
  "GUTranID": "15010615234271604",
  "TenentID": 39,
  "CompanyID": 42,
  "ViewObjectID": 0,
  "ObjectID": null,
  "Status": 2,
  "OrderDetails": [
    {
      "GUPromtionID": "15010519341113508",
      "GUOrderID": "15010615234271703",
      "ChangeID": 0,
      "GUPackageID": "14100112243589402",
      "startdate": "2015-01-06T00:00:00" 
    }
]

I need to get the current DateTime and assign to the CreateDate. How can i do that?

like image 954
Sajeetharan Avatar asked May 19 '15 13:05

Sajeetharan


People also ask

Can we use curl command in shell script?

The curl command transfers data to or from a network server, using one of the supported protocols (HTTP, HTTPS, FTP, FTPS, SCP, SFTP, TFTP, DICT, TELNET, LDAP or FILE). It is designed to work without user interaction, so it is ideal for use in a shell script.

How do you send a POST request on curl?

To make a POST request with Curl, you can run the Curl command-line tool with the -d or --data command-line option and pass the data as the second argument. Curl will automatically select the HTTP POST method and application/x-www-form-urlencoded content type for the transmitted data.

What is curl commands?

What Is the curl Command? curl (short for "Client URL") is a command line tool that enables data transfer over various network protocols. It communicates with a web or application server by specifying a relevant URL and the data that need to be sent or received.


1 Answers

You can do it like this:

DATE_ISO=$(date +"%Y-%m-%dT%H:%M:%S")
...
curl -XPUT 'http://localhost:9200/nondomain_order/orders/'+$number+'' -d '{
...
   "CreateDate": "'"$DATE_ISO"'",
...
}'
like image 175
Val Avatar answered Oct 21 '22 18:10

Val