Bash Script: Error when using variable in curl data - bash

I've been working on creating a script utilizes curl and variables. Doing some searches I found how to place a variable in the data portion of the curl but now I'm getting errors from the curl command.
(Some parts of the code removed to keep passwords out, everything is working except for entering the data into the --data portion of curl)
curlData=$(cat <<EOF
{
"rebootClient": false,
"createPseudoClientRequest": {
"registerClient": true,
"clientInfo": {
"clientType": 0,
}
},
"packages": [
{
"packageId": 702,
"packageName": "File System",
"packageId": 51,
"packageName": "MediaAgent"
}
],
},
"entities": [
{
"clientId": 0,
}
]
}
EOF
)
shopt -u nocasematch #Sets options back to being case sensitive
echo "$csName"
curl -vv --location --request POST "http://$csName:81/SearchSvc/CVWebService.svc/InstallClient" --header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--header "Authtoken: $token" \
-d "$curlData"
Doing a bash -x everything looks correct
"rebootClient": false,
"createPseudoClientRequest": {
"registerClient": true,
"clientInfo": {
"clientType": 0,
}
},
"packages": [
{
"packageId": 702,
"packageName": "File System",
"packageId": 51,
"packageName": "MediaAgent"
}
],
"clientAuthForJob": {
},
"entities": [
{
"clientId": 0,
"clientName": "srybrcost",
}
]
}'
But every time I get Request body is empty or format is invalid

Related

How to resolve 'curl: option --data-raw: is unknown' error

I am trying to run shell script with a curl command as below with --data-raw as body.
curl --location --request POST '<URL>' \
--header 'Content-Type: application/json' \
--data-raw '{
"blocks": [
{
"type": "header",
"text": {
"type": "plain_text",
"text": "Help Text",
"emoji": true
}
},
{
"type": "divider"
},
]
}'
Output:
curl: option --data-raw: is unknown
curl: try 'curl --help' or 'curl --manual' for more information
I couldn't find any error with the json validator. Please suggest a solution. TIA.
Here, you have 2 issue.
your JSON is invalid, the , line 14 need to be removed
use --data and a heredoc :
curl --location --request POST '<URL>' \
--header 'Content-Type: application/json' \
--data "#/dev/stdin"<<EOF
{
"blocks": [
{
"type": "header",
"text": {
"type": "plain_text",
"text": "Help Text",
"emoji": true
}
},
{
"type": "divider"
}
]
}
EOF
Here documents:
cat <<EOF followed by several lines of text, followed by the literal string EOF on a new line, NOT indented. The portion between the EOFs is passed to the command as standard input. If 'EOF' is 'quoted', substitutions WON'T be done; otherwise they are. See <<- for the indented variety (not recommended, need TABs).

How to write json output to a CSV file using jq in a bash script?

I have a JSON response from a API call as below and would like to write this to CSV file through a shell script.
JSON response:
{
"maxResults": 50,
"startAt": 0,
"isLast": true,
"values": [
{
"id": 1986,
"startDate": "2020-01-27T20:15:30.094Z",
"endDate": "2020-02-24T20:15:00.000Z"
},
{
"id": 1987,
"startDate": "2020-02-24T20:48:40.618Z",
"endDate": "2020-03-15T20:48:00.000Z"
},
{
"id": 1988,
"startDate": "2020-03-16T17:46:16.846Z",
"endDate": "2020-04-24T17:46:00.000Z"
},
{
"id": 1989,
"startDate": "2020-04-20T17:59:30.920Z",
"endDate": "2020-06-10T17:59:00.000Z"
}
]
}
the CSV file should look something like below
1986,2020-01-27T20:15:30:094Z,2020-02-24T20:15:00:000Z
1987,2020-02-24T20:48:40.618Z,2020-03-15T20:48:00.000Z
1988,2020-03-16T17:46:16.846Z,2020-04-24T17:46:00.000Z
1989,2020-04-20T17:59:30.920Z,2020-06-10T17:59:00.000Z
I have tried using jq but unable to figure out the correct way to write to csv
curl -X GET \
--header 'Authorization: Basic <token>' \
--header 'Content-Type: application/json'\
"$url" | jq -r '[.values[].id, .values[].startDate, .values[].endDate] | #csv ' >> dates.csv
Any inputs would be helpful.
this may depend on the api endpoint, but you could just request for CSV when calling your API. curl -X GET -H "Content-Type: text/csv" 'https://some.api.endpoint.com/api/request.csv' so you get the CSV response instead of having to convert it yourself.

Loop through multiple cURL api request body parameters using Bash script

I want to convert multiple audio files to text using Google Cloud's Speech Recognize API.
I successfully transcribed one audio file called '1.flac'...
Request:
curl -s -H "Content-Type: application/json" \
-H "Authorization: Bearer ACCESSTOKEN" \
https://speech.googleapis.com/v1/speech:recognize \
-d '
{"config": {"languageCode": "pt-BR", "audioChannelCount": 2},"audio":{"uri": "gs://PROJECTID/1.flac"}}
'
Response:
{
"results": [
{
"alternatives": [
{
"transcript": "cat",
"confidence": 0.9999999
}
]
}
]
}
I successfully generated multiple lines for the data/body portion of the above request...
Request:
for i in 1 2 3
do
echo "{\"config\": {\"languageCode\": \"pt-BR\", \"audioChannelCount\": 2},\"audio\":{\"uri\": \"gs://PROJECTID/$i.flac\"}}"
done
Response:
{"config": {"languageCode": "pt-BR", "audioChannelCount": 2},"audio":{"uri": "gs://PROJECTID/1.flac"}}
{"config": {"languageCode": "pt-BR", "audioChannelCount": 2},"audio":{"uri": "gs://PROJECTID/2.flac"}}
{"config": {"languageCode": "pt-BR", "audioChannelCount": 2},"audio":{"uri": "gs://PROJECTID/3.flac"}}
How can I combine these two scripts, so that the curl API executes once for each of the three files, with one response like this:
{
"results": [
{
"alternatives": [
{
"transcript": "cat",
"confidence": 0.9999999
}
]
}
]
}
{
"results": [
{
"alternatives": [
{
"transcript": "dog",
"confidence": 0.9999999
}
]
}
]
}
{
"results": [
{
"alternatives": [
{
"transcript": "horse",
"confidence": 0.9999999
}
]
}
]
}
Your code is almost right.
for i in 1 2 3
do
curl -s -H "Content-Type: application/json" \
-H "Authorization: Bearer ACCESSTOKEN" \
-d '{"config": {"languageCode": "pt-BR", "audioChannelCount": 2}
,"audio":{"uri": "gs://PROJECTID/'$i'.flac"}}' \
https://speech.googleapis.com/v1/speech:recognize
done
I put URL at the end, because usually, options come before arguments
The value of -d is composed of 3 parts ['...'] [$i] ['...'] chained together. This allows the expansion of [$i]

Parsing Json in Json

I'm working in a Jenkinsfile and trying to parse the following return output. I can get the uuid but I can't get name. Looking for some guidance.
Jenkinsfile
node('ansible'){
stage('Get VM List'){
def content = sh (returnStdout: true, script: "curl -X GET --header 'Content-Type: application/json' --header 'Accept: application/octet-stream' 'http://someurlapi'").trim()
def vmList = readJSON text: content;
//Works
echo vmList[0].uuid
}}
Return Output
[
{
"num": XX,
"ip": "XX.XX.XX.XX",
"type": "KVM",
"name": "machinename",
"state": "Running",
"ram": 4096,
"ram-display": "4 GiB",
"zpool": {
"name": "zpool",
"compression": "lz4",
"mountpoint": "\/mnt",
"mounted": true
},
"uuid": "d7622bd3-ed3d-5000-ae01-89ab294933r1",
"autostart": false,
"cpu": 2
}]
I figured it out I changed it to
echo vmList[0]["name"]

Does parse.com support batch operations for push notifications?

I am attempting to do a batch push notification request using the parse rest API.
curl -X POST
-H "X-Parse-Application-Id: redacted"
-H "X-Parse-REST-API-Key: redacted"
-H "Content-Type: application/json"
-d '{
"requests": [{
"method": "POST",
"path": "/1/push/",
"body": {
"channels": ["redacted"],
"deviceType": "ios",
"badge": 1,
"data": {
"alert": "Hello",
"badge": 1,
"key": "status"
}
}
}]
https://api.parse.com/1/batch
And am receiving the error:
{"code":107,"error":"Method 'POST' to '/1/push/' not supported in batch operations."}
You are already indicating POST in CURL. The Parse push api is not batch. Push is sent to registered devices in Parse (associated with a certificate from Apple) and those matching your push criteria. Your push message should look more like this:
curl -X POST \
-H "X-Parse-Application-Id: " \
-H "X-Parse-REST-API-Key: " \
-H "Content-Type: application/json" \
-d '{
"where": {
"channels": {
"$in": ["channel1","channel2","channel3"]
},
"deviceType": "ios"
},
"data": {
"alert": "Alert message here."
}
}' \
https://api.parse.com/1/push
There are various examples in the docs. The above is from the help forum.

Resources