Wednesday, 2 December 2020

CRUL USEFUL SAMPLE AND CURL scripts

 Useful CURL:

// POST

curl -i \

-H "content-type:application/json" \

-H "content-length:2" \

-H "Authorization:<JWT_TOKEN>" \

-X POST <URL> \

--trace-ascii /dev/stdout >> test.txt


// POST with data

curl -d '{"key1":"value1", "key2":"value2"}' -H "Content-Type: application/json" -X POST http://localhost:3000/data



// PUT

curl -i \

-u username:pwd \  //simple user name and pwd

-H "content-type:application/json" \

-X PUT -d '{"password":"test","tags":"administrator"}' \

http://192.168.20.23:15672/api/test \

--trace-ascii /dev/stdout > outputfile.txt


// GET

curl -i \

-H "content-type:application/json" \

-H "Authorization: <JWT_TOKEN>" \

-X GET https://myendpoint \

--trace-ascii /dev/stdout >> result.txt>&1


// List of flags and commands

curl --help


// Shell. sh

make # number of curl calls, calculate average

------------------------------------------------------------------------------------------

-------------------------------------------------------------------------------------------

#!/bin/bash


function usage() {

echo "Usage:  $0 host count size token"

echo "Example: $0 api.com/api/test 10 jwtToken";

}

# Check number of argument is equal to 3

if [ $# -ne 3 ]; then

usage;

exit;

fi


host=$1

count=$2

token=$3


let i=$count-1

tot=0

while [ $i -ge 0 ];

do

res=$(curl  -i \

-H "content-type:application/json" \

-H "Accept-Encoding:gzip" \

-H "content-length:2" \

-H "Authorization:$token" \

-w "$i: %{time_total} %{http_code} %{size_download} %{url_effective}\n" -o "/dev/null" -s https://$host)

echo $res


val=$(echo $res | cut -f2 -d' ')

# Add Float without bc

tot=$(awk "BEGIN {print $tot+$val}")

let i=i-1

done


# avg=`echo " ${tot}/${count}"`

# Divide float without bc

avg=$(awk "BEGIN {print $tot/$count}")

echo "   ........................."

echo "   AVG:  $avg sec"

------------------------------------------------------------------------------------------

To execute ./thisShell.sh  <api_end_point>  <number_of_api_call>  <jwt_token> > result.txt 2>$1

-------------------------------------------------------------------------------------------





No comments:

Post a Comment