Can I define an object as a variable in a shell script? - bash

I know how to store a string as a variable, for example: API="http://localhost:4741"
However, for the sake of a CURL request I would like to be able to store on object as a variable that I can access values on, something like OBJ="{name : Joe}". Is this possible?
Right now my CURL request looks like this:
curl --include --request POST localhost:3000/scrape \
--header "Content-Type: application/json" \
--data '{
"url": "http://www.oddsshark.com/stats/gamelog/basketball/nba/20736",
"team": "LA Clippers"
}'
I would like to be able to do something like this, using a dictionary or an object:
TEAM=( ["Clippers"]="http://www.oddsshark.com/stats/gamelog/basketball/nba/20736" )
curl --include --request POST localhost:3000/scrape \
--header "Content-Type: application/json" \
--data '{
"url": "http://www.oddsshark.com/stats/gamelog/basketball/nba/20736",
"team": "${TEAM[Clippers]}"
}'

Related

Automate cURL to run through a list of ID's

I am attempting to run through a list of 10 contact ID's every ten minutes with cURL
This is the code I am trying to run
curl --request POST \
--url https://youraccountname.api-us1.com/api/3/contactAutomations \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--header 'api-token: test' \
--data '
{
"contactAutomation": {
"contact": THE VARIABLE,
"automation": 23
}
}'
The list of contact variables could just be line items in a text file.
Is this something a bash script with cron could handle - and it just deleted the 10 IDs it runs?
Or is this something I would need to use python and a database to run?
This is really a one off thing - so a cron with a script would be easiest since it doesn't need to be used more than one time through.
If you have a constant list of IDs:
for id in 123 456 555 777
do
curl --request POST \
--url https://youraccountname.api-us1.com/api/3/contactAutomations \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--header 'api-token: test' \
--data "
{
\"contactAutomation\": {
\"contact\": $id,
\"automation\": 23
}
}"
done

Not able to replace the value of the variable inside expression in bash script

I am trying to run a bash script, where I would like to make POST calls in a for loop as follows:
for depId in "${depIds[#]}"
do
echo "$depId" <--------------------------------- THIS IS PRINTING PROPER VALUE
curl 'https://student.service.com/api/student' \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
-H 'Cookie: UISESSION=abcd' \
--data-raw '{"name":"Student Name","description":"Dummy","depId":$depId}' \ <---- HERE I CANNOT GET THE VALUE OF THE VARIABLE
--compressed
echo "$content"
done
As mentioned above, I cannot get the value of the department id in the URL, with the above form, I am getting a Request Malformed exception. I have even tried with ${depId}, but no luck.
Could anyone please help here ?
Try flipping your quotes around the variable.
--data-raw '{"name":"Student Name","description":"Dummy","depId":'"$depId"'}' \

Bash script - cURL with variables

I'm trying to get a cURL script to input my variables in a --data section. I'm fairly new to this, but it's just inserting the variables names.
The background is this script is called to hit an API in our ticketing system to create a new job. I am finding the ticket that is created has the subject "${DESCRIPTION}" and not "problem description".
#!/bin/bash
# This will log a ticket in Ticketing System
DESCRIPTION='Problem Description'
SUBJECT='Problem Subject'
curl --location --request POST 'https://XXXXX.domain.com/helpdesk/tickets.json' \
--header 'Content-Type: application/json' \
--header 'Authorization: Basic XXXXXXXXXXXXXXXXXXXXXXXXXXXXX' \
--data '{
"helpdesk_ticket":
{
"description": "${DESCRIPTION}",
"subject": "${SUBJECT}",
"email": "email#domain.com",
"priority": 1,
"status": 2
},
"cc_emails": ""
}'
As per always, got this working just after I posted....
The solution was to wrap the variable in "'".
#!/bin/bash
# This will log a ticket in Ticketing System
DESCRIPTION='Problem Description'
SUBJECT='Problem Subject'
curl --location --request POST 'https://XXXXX.domain.com/helpdesk/tickets.json' \
--header 'Content-Type: application/json' \
--header 'Authorization: Basic XXXXXXXXXXXXXXXXXXXXXXXXXXXXX' \
--data '{
"helpdesk_ticket":
{
"description": "'"$DESCRIPTION"'",
"subject": "'"$SUBJECT"'",
"email": "email#domain.com",
"priority": 1,
"status": 2
},
"cc_emails": ""
}'

curl: 400 Bad Request. The browser (or proxy) sent a request that this server could not understand

I'm getting "400 Bad Request. The browser (or proxy) sent a request that this server could not understand." for the curl command. Curl command is as below, can someone please help?
token=jfkdjfdikdjydve83hhd54rsfdghsghktwhdh87
payload_file=test_fortify_scan.zip
curl -X POST https://dnsname/api/scan \
-H "authorization: Bearer $token" \
-H 'content-type: application/json' \
-d '{"projVer": "dev", "language": "java",
"payloadFile":"$payload_file", "emailList": "myemail#mail.com"}'
Looks like quotes problem. Look at Using curl POST with variables defined in bash script functions
token="jfkdjfdikdjydve83hhd54rsfdghsghktwhdh87"
payload_file="test_fortify_scan.zip"
curl -X POST https://dnsname/api/scan \
-H "authorization: Bearer $token" \
-H 'content-type: application/json' \
-d '{"projVer": "dev", "language": "java",
"payloadFile":"'"$payload_file"'", "emailList": "myemail#mail.com"}'

rest api openam add user parametrised

hello I have this script in bash:
first part : authentication against server - works fine.
second part: adds user - and this works fine when I define user directly in curl code - but when I want to add user from parameters.. then I got a message error:
"{"code":400,"reason":"Bad Request","message":"The request could not
be processed because the provided content is not valid
JSON","detail":"Unexpected character ('$' (code 36)): expected a valid
value (number, String, array, object, 'true', 'false' or 'null')\n at
[Source: org.apache.catalina.connector.CoyoteInputStream#3a248e6a;
line: 2, column: 18]"}"
Any ideas how can I pass values to this script ? :)
Regards
#!/bin/bash
adm_user="admin"
adm_pass="secret"
user_name=""
user_pass=""
auth_url="http://url/OpenAM-11.0.0/json/authenticate"
add_user_url="http://url/OpenAM-11.0.0/json/users/?_action=create"
session_id=$(curl \
--request POST --header "X-OpenAM-Username: $adm_user " \
--header "X-OpenAM-Password: $adm_pass" \
--header "Content-Type: application/json" \
--data "{}" $auth_url | cut -d"\"" -f4 )
sleep 1
curl \
--request POST \
--header "iplanetDirectoryPro: $session_id" \
--header "Content-Type: application/json" \
--data \
'{
"username": "$1",
"userpassword": "secret12",
"mail": "bjensen#example.com"
}' \
$add_user_url
OK, mates I got the answer:
'{
"username": "'"$1"'",
"userpassword": "'"$2"'" ,
"mail": "bjensen#example.com"
}' \

Resources