Bash script to insert variables into a json payload (slack webhook) question [duplicate] - json

This question already has answers here:
Curl command issue in bash script because of string variable containing quotes [duplicate]
(2 answers)
Closed 3 years ago.
I am trying to insert a variable into my json payload (working in shell script) but I am unsure of how to escape the characters properly
I've tried many different escape methods but I'm a total noob at it and I either return the literal string or it doesn't run
SLACK_ALERT_WEBHOOK=desiredurl
curl -X POST -H 'Content-type: application/json' --data '{"text": "*Daily Webhook Verification*", "attachments": [
{
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "Slack post failed for webhook, please investigate: $SLACK_ALERT_WEBHOOK"
}
}
]
}
]}' "$SLACK_ALERT_WEBHOOK"
I just want to insert the value of SLACK_ALERT_WEBHOOK into this portion of the code "text": "Slack post failed for webhook, please investigate: $SLACK_ALERT_WEBHOOK but it either doesn't run or returns the literal string. I have the bottom "$SLACK_ALERT_WEBHOOK" working successfully at the bottom to send to my desired slack channel so I'm not worried about that.
I've got it working thanks to tripleee:
curl -X POST -H 'Content-type: application/json' --data "{\"text\": \"*Verification*\", \"attachments\": [{\"blocks\": [{\"type\": \"section\",\"text\": {\"type\": \"mrkdwn\",\"text\": \"$SLACK_ALERT_WEBHOOK\"}}]}]}" $SLACK_ALERT_WEBHOOK

Try this:
SLACK_ALERT_WEBHOOK=desiredurl
$(curl -X POST -H 'Content-type: application/json' --data '{"text": "*Daily Webhook Verification*", "attachments": [
{
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "Slack post failed for webhook, please investigate: `echo $SLACK_ALERT_WEBHOOK`"
}
}
]
}
]}' "`echo $SLACK_ALERT_WEBHOOK`")
Using the echo <<variable>> it should just run that as a command. I tested it on my own server, but might work differently on yours. Hope it helps.

Related

Curl - Encode file as url query

Curl - Encode file as url query
I'm trying to send message to Amazon SQS queue with payload read from file using curl
So far I've got working curl to send message to queue but with harcoded value like:
curl --request POST \
--url 'http://localhost:9324/queue/price-import?Action=SendMessage&MessageBody=%7B%0A%09%22id%22%3A%20%22f23399af-a384-4ed3-bac0-386a01804d83%22%2C%0A%09%22de%22%3A%20%7B%0A%09%09%22price%22%3A%2010.59%0A%09%7D%2C%0A%09%22nl%22%3A%20%7B%0A%09%09%22price%22%3A%20107.99%0A%09%7D%2C%0A%09%22pl%22%3A%20%7B%0A%09%09%22price%22%3A%2012.15%0A%09%7D%0A%7D'
And payload I send is:
{
"id": "f23399af-a384-4ed3-bac0-386a01804d83",
"de": {
"price": 10.59
},
"nl": {
"price": 107.99
},
"pl": {
"price": 12.15
}
}
How can I modify this curl to omit harcoded value and instead put path to file with .json extension?
Desired output
curl --request POST \
--data-urlencode http://localhost:9324/queue/price-import?Action=SendMessage&MessageBody=#input.json
What I already tried
curl --request POST -data-urlencode http://localhost:9324/queue/price-import?Action=SendMessage&MessageBody=#input.json
curl --request POST -data-urlencode input.json http://localhost:9324/queue/price-import
curl --request POST -data-urlencode "sendbody#input.json" http://localhost:9324/queue/price-import
curl --request POST --data-urlencode SendBody#input.json --url http://localhost:9324/queue/price-import?Action=SendMessage
Resources I found
Unix cURL POST to specific variable using contents from a file
POSTing a file's contents with CURL
curl.1 the man page
Answering to my own question based on this answer:
How to urlencode data for curl command?
This worked for me:
curl \
--data-urlencode MessageBody#input.json \
--data-urlencode "Action=SendMessage" \
http://localhost:9324/queue/price-import
and my input.json file:
{
"id": "f23399af-a384-4ed3-bac0-386a01804d83",
"de": {
"price": 10.59
},
"nl": {
"price": 107.99
},
"pl": {
"price": 12.15
}
}

Add query to training data using CURL

In the Watson API documentation, the following is given for adding query to training data using CURL:
curl -X POST -u "{username}":"{password}" -H "Content-Type: application/json" -d
'{
"natural_language_query": "who is keyser soze",
"filter": "text:criminology",
"examples": [
{
"document_id": "adaf50f1-2526-4fad-b670-7d6e8a42e6e6",
"relevance": 2
},
{
"document_id": "63919442-7d5b-4cae-ab7e-56f58b1390fe",
"cross_reference": "my_id_field:14",
"relevance": 4
}
]
}' "https://gateway.watsonplatform.net/discovery/api/v1/environments/{environment_id}/collections/{collection_id}/training_data?version=2018-03-05"
However much I try or use multiple libraries, it does not seem to have the correct JSON format (even when JSON validators have confirmed).
Has anyone tried using this method to add queries?

curl: (3) [globbing] bad range specification in column 3

Im trying to index a simple json data in solr using curl. When i use the command, it says
"curl -X POST -H 'Content-Type:application/json'-d http://localhost:8983/solr/informationretrieval/update/json/docs '[{"id":"1","title":"Doc 1"},{"id":"2","title":"Doc 2"}]'
{"responseHeader":{"status":0,"QTime":1}}
curl: (3) [globbing] bad range specification in column 3"
I have tried removing quotes, tried -g and --globoff etc but every time there are errors like illegal port number. Can anyone please help?
Adding Multiple JSON Documents
curl -X POST -H 'Content-Type: application/json' 'http://localhost:8983/solr/informationretrieval/update' --data-binary '
[
{
"id": "1",
"title": "Doc 1"
},
{
"id": "2",
"title": "Doc 2"
}
]'

What are the correct params for Google Cloud Vision JSON Data

I'm getting this error:
error": { "code": 400, "message": "Invalid value at 'requests[0].image.content' (TYPE_BYTES), \"000002.jpg\"", "status": "INVALID_ARGUMENT", "details": [ { "#type": "type.googleapis.com/google.rpc.BadRequest"
And it may have something to do with the encoding of the images, not sure. I am sending Google a bunch of .jpgs.
Note that this is similar to this question, but that one doesn't help a whole lot - there's no answer.
Here's my JSON:
for f in $FILES
do
echo "Original file name is $f"
response=$(curl -v -H "Accept: application/json" -H "Content-type:
application/json" -X POST -d '{"requests":[
{
"image":{
"content":"'"$f"'"
},
"features":[
{
"type":"LABEL_DETECTION",
"maxResults":3
}
]
}]}' $baseURL)
echo $response
done
This is the first image I send it.
Any thoughts on what's causing this?
You need to send the base64 encoded representation of the image, not the filename.

cURL Response JSON RestAPI

I need to delete some wrong data, inserted in a lot of processes, and I need to figure if this is possible with cURL and rest API, with a script in sh, batch or something like this:
curl -u admin:admin -i -H "Accept: application/json" -X GET "http://json_bpm.com/wle/v1/service/6112449?action=getData&fields=context"
First I just need the result map.
Output:
{"status":"200","data":{"result":"{\"context\":{\"name\":\"xxx\" (...)
"resultMap":{"context":{"name\":"xxx\" (...) }}}
Because I need to remove the userDelete array (see below) for thousands of processes, and set this again using curl. If you know how to remove arrays from JSON too, you're the man. :)
{
"context": {
"name": "Change Process",
"startUser": {
"user": "0001"
},
"endUser": {
"user": "0001"
},
"userDelete": {
"user": "0002"
},
"origin": "GUI",
"userAction": "Change Process"
}
}