rest api openam add user parametrised - bash

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"
}' \

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": ""
}'

Loop through list for curl requests in bash

I have a bash script that sends a curl request and displays the response.
#!/bin/bash
token=$(curl -k -X GET \
'https://v.mytesting.io/oauth/token?grant_type=password&username=user1&password=123' \
-H 'Authorization: Basic 12345678' \
-H 'Host: v.mytesting.io.io')
v=$( jq -r ".access_token" <<<"$token" )
ts=$(curl -k -X POST \
https://timeseries.mytimeseries.io/v5/time_series/query \
-H 'Authorization: Bearer '"$v" \
-H 'Content-Type: application/json' \
-H 'Host: timeseries.mytimeseries.io' \
-H 'tenant: 123-123-123' \
-d '{"operation" : "raw","responseFormat" : "kairosDB","startTime": "1d-ago","stopTime": "now","tagList" : [ {"tagId" : "V.S.23164117.AVG.10M"}]}')
p=$(jq '.queries[].sample_size, .queries[].results[].name' <<<"$ts")
echo "$p"
My current output is just a value and the name of the tagId.
My query only allows for 1 tagId ( you can see above )
I want to be able to set a list of tagId's.
Then when I run this script it should loop through the list of tagId's and execute the curl request replacing the V.S.23164117.AVG.10M with each value
in the list.
Then output the entire list of results into a file.
list would be like so - (I would love to be able to enter this list into a seperate file and the bash script calls that file. Sometimes this list can be a few hundred lines.
V.S.23164117.AVG.10M
V.S.23164118.AVG.10M
V.S.23164119.AVG.10M
V.S.23164115.AVG.10M
V.S.23164114.AVG.10M
output would like look so.
value tagId
value tagId
value tagId
100 V.S.23164117.AVG.10M
etc..
thank you for any help
You can loop over list of tags using a small script. I'm not 100% clean of the output format. You can change the 'echo' to match the required format.
Note minor change to quotes to allow variable expansion in the body.
The tags will be stored in a file, for examples, tags.txt
V.S.23164117.AVG.10M
V.S.23164118.AVG.10M
V.S.23164119.AVG.10M
And the script will be use the file
#! /bin/bash
# Use user defined list of tags
tags=tags.txt
token=$(curl -k -X GET \
'https://v.mytesting.io/oauth/token?grant_type=password&username=user1&password=123' \
-H 'Authorization: Basic 12345678' \
-H 'Host: v.mytesting.io.io')
v=$( jq -r ".access_token" <<<"$token" )
for tag in $(<$tags) ; do
ts=$(curl -k -X POST \
https://timeseries.mytimeseries.io/v5/time_series/query \
-H 'Authorization: Bearer '"$v" \
-H 'Content-Type: application/json' \
-H 'Host: timeseries.mytimeseries.io' \
-H 'tenant: 123-123-123' \
-d '{"operation" : "raw","responseFormat" : "kairosDB","startTime": "1d-ago","stopTime": "now","tagList" : [ {"tagId" : "'"$tag"'"}]}')
p=$(jq '.queries[].sample_size, .queries[].results[].name' <<<"$ts")
echo "$tag $p"
done

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

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]}"
}'

Resources