Json object not escaped properly in bash [duplicate] - bash

This question already has answers here:
Difference between single and double quotes in Bash
(7 answers)
Closed 3 years ago.
This is my script
#!/bin/bash
set -x
USERNAME="someUser"
curl -H "Content-Type: application/json" -X POST -d '{"auth":{ "client": $USERNAME, "password": "somePassword" }, "messages": { "UpdateProduct": {} }}' http://someDomain.com/services/json
it just becomes as below, it did not replace $USERNAME with someUser
curl -H "Content-Type: application/json" -X POST -d '{"auth":{ "client": $USERNAME, "password": "somePassword" }, "messages": { "UpdateProduct": {} }}' http://someDomain.com/services/json
but if i put USERNAME in, it works
curl -H "Content-Type: application/json" -X POST -d '{"auth":{ "client": "someUser", "password": "somePassword" }, "messages": { "UpdateProduct": {} }}' http://someDomain.com/services/json

Single quotes don't expand variables. You can use something like
-d '{"auth":{ "client": '"$USERNAME"', "password"...
but it can still break if the username contains a double quote.
The cleanest way is to use a tool that understands JSON, e.g. jq:
$ USERNAME='"a"' jq '.username|=env.USERNAME' <<< '{"username":"..."}'
{
"username": "\"a\""
}

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).

CURL variables and problems parsing JSON [duplicate]

This question already has answers here:
When to wrap quotes around a shell variable?
(5 answers)
Closed 1 year ago.
My script is creating pull requests using the github API:
./my-script.sh my-repo my-branch
#!/bin/bash
Repo=$1
Branch=$2
cd $Repo
get_data() {
cat <<EOF
{
"title": "PR title",
"head": $Branch,
"base": "development",
"body": "PR description"
}
EOF
}
echo $(get_data) # <----------------- I can see my value of the variable $Branch here
curl -X POST \
-H "Accept: application/vnd.github.v3+json" \
-d "$(get_data)" \ # <----------------- But here I'm facing "Problems parsing JSON"
-u my_user:my_token \
https://api.github.com/repos/my_user/$Repo/pulls
open https://github.com/my_user/$Repo/pulls
How can I set my variable into the curl correctly?
$Branch needs to be quoted as well:
get_data() {
cat <<EOF
{
"title": "PR title",
"head": "$Branch",
"base": "development",
"body": "PR description"
}
EOF
}

Get zabbix graph to png via API

I have a login script as follows:
curl --location --request POST 'https://zabbixUrl.com/api_jsonrpc.php' \
--header 'Authorization: Basic bWlndWVsLmh1cnRhZG86MTczM295ZmQxVg==' \
--header 'Content-Type: application/json' \
--data-raw '{
"jsonrpc": "2.0",
"method": "user.login",
"params": {
"user": "user",
"password": "pass"
},
"id": 1
}'
Which returns:
{"jsonrpc":"2.0","result":"1g1hd43j4d3jd4jsl4n35b4211n1d2e2","id":1}
With this Api token (result), I can execute all the Zabbix's methods, for instance:
curl --location --request POST 'https://zabbixUrl.com/api_jsonrpc.php' \
--header 'Authorization: Basic bWlndWVsLmh1cnRhZG86MTczM295ZmQxVg==' \
--header 'Content-Type: application/json' \
--data-raw '
{
"jsonrpc": "2.0",
"method": "screenitem.get",
"params": {
"output": "extend",
"screenids": "258"
},
"auth": "1g1hd43j4d3jd4jsl4n35b4211n1d2e2",
"id": 1
}'
Now I am trying to obtain a graph in PNG format. I can view this graph in PNG format after inserting the user and pass in the intermediate screen:
Now what I am trying to do is to get this PNG graph via API. My approach is the following (ps: the url included is the same one as the one used on the browser, where it does work):
curl --header 'Authorization: Basic bWlndWVsLmh1cnRhZG86MTczM295ZmQxVg==' --data-raw 'auth:1g1hd43j4d3jd4jsl4n35b4211n1d2e2' http://zabbixUrl.com/chart2.php?graphid=123456&period=604800&stime=1614012539
Using this form, I get a 401 error. I guess that it is not correctly detecting the token.
Therefore, my question is, how can I obtain the PNG of this Zabbix's graph via API? How can I do it so it correctly detects the token?
When you authenticate to the API you use a token, when you authenticate to the GUI you need a cookie. Using curl, you need the --cookie option, see Save cookies between two curl requests.
In python requests, you would use a Session: see zabbix-gnomes's zgetgraph.py for a python solution.
A working example in Zabbix 5.0:
auth=$(curl -s --data '{ "jsonrpc": "2.0", "method": "user.login", "params": { "user": "...", "password": "..." }, "id": 1 }' --header 'Content-Type: application/json' https://.../zabbix/api_jsonrpc.php | jq -r .result)
curl --cookie "zbx_sessionid=$auth" "https://.../zabbix/chart2.php?graphid=517&from=now-2d&to=now&height=201&width=1717&profileIdx=web.charts.filter&_=up0bkgs0" -o file.png
I found the solution:
import requests
import json
import shutil
​
##### GET CREDENTIALS #####
​
USER = "user"
PASSWORD = "pass"
URL_AUTH = "https://url.com/api_jsonrpc.php"
HEADER_AUTH = {
'Authorization': 'Basic bWlndWVsLmh1cnRhZG86MTczM295ZmQxVg==',
'Content-Type': 'application/json',
}
DATA_AUTH = """
{
"jsonrpc": "2.0",
"method": "user.login",
"params": {
"user": "%s",
"password": "%s",
"userData": true
},
"id": 1
}
""" % (USER, PASSWORD)
respAuth = requests.post(URL_AUTH, headers=HEADER_AUTH, data=DATA_AUTH)
zabbixSessionId = json.loads(respAuth.content.decode('utf8').replace("'", '"'))["result"]["sessionid"]
##### GET PNG #####
URL_CHART = "https://url.com/chart2.php?graphid=12345&period=7200"
HEADER_CHART = {
'Authorization': 'Basic bWlndWVsLmh1cnRhZG86MTczM295ZmQxVg==',
'Content-Type': 'image/png',
'Cookie': 'zbx_sessionid=%s' % (zabbixSessionId)
}
PNG_PATH = "./image.png"
respChart = requests.request("POST", URL_CHART, headers=HEADER_CHART, stream=True)
with open(PNG_PATH, 'wb') as out_file:
shutil.copyfileobj(respChart.raw, out_file)

Shell Script: curl malformed body

I'm getting this error message:
json parse error: invalid character 'E' after top-level value (202020207b202020202020226b696e64223a2022536563726574222c2020 ...)
I'm launching a curl statement from my shell script:
curl -k \
-X PUT \
-d #- \
-H "Authorization: Bearer $TOKEN" \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
"$SERVER_URL/api/v1/namespaces/$NAMESPACE/secrets/t-secret" <<'EOF'
{
"kind": "Secret",
"apiVersion": "v1",
"data": {
"rabbit-password": "fromcontainer"
}
}
EOF
Any ideas?
You cannot indent the closing EOF with spaces.
curl -k \
-X PUT \
-d #- \
-H "Authorization: Bearer $TOKEN" \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
"$SERVER_URL/api/v1/namespaces/$NAMESPACE/secrets/t-secret" <<'EOF'
{
"kind": "Secret",
"apiVersion": "v1",
"data": {
"rabbit-password": "fromcontainer"
}
}
EOF

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