Shell Script: curl malformed body - shell

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

Related

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)

Classroom Orphaned by deleted teacher account

My predecessor was in the habit of deleting users from gsuite. This has resulted in 18 courses that I cannot figure out how to change from ACTIVE to ARCHIVED.
GET https://classroom.googleapis.com/v1/courses/#######?key=[YOUR_API_KEY] HTTP/1.1
{
"id": "#######",
"name": "Course Name",
"ownerId": "#####################",
"creationTime": "2014-01-01T00:00:00.000Z",
"updateTime": "2015-01-01T00:00:00.000Z",
"enrollmentCode": "xx#xxx#",
"courseState": "ACTIVE",
"alternateLink": "https://classroom.google.com/c/XXX#XXXxXxxx",
"teacherGroupEmail": "Course_Name_teachers_#xXxXx#domain.com",
"courseGroupEmail": "Course_Name--#xXxXx#comain.com",
"teacherFolder": {
"id": "#X#Xx#xxX#xXXxXXXX#xXxXXxxXx"
},
"guardiansEnabled": false
}
Trying to PATCH the class to ARCHIVED:
curl --request PATCH \
'https://classroom.googleapis.com/v1/courses/#######?updateMask=courseState&key=[YOUR_API_KEY]' \
--header 'Authorization: Bearer [YOUR_ACCESS_TOKEN]' \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--data '{"courseState":"ARCHIVED"}' \
--compressed
Returns error 500:
{
"error": {
"code": 500,
"message": "Internal error encountered.",
"status": "INTERNAL"
}
}
It's a bug that Google seems to be aware of and has no solution at this time.
https://issuetracker.google.com/issues/36760244#comment13

Empty result of search

Use code:
curl \
'https://www.googleapis.com/youtube/v3/search?
part=snippet&channelId=UCsA_vkmuyIRlYYXeJueyIJQ&eventType=live&maxResults=25&type=video&key=[MY_API_KEY]' \
--header 'Authorization: Bearer [YOUR_ACCESS_TOKEN]' \
--header 'Accept: application/json' \
--compressed
and in result:
{
"kind": "youtube#searchListResponse",
"etag": "\"8jEFfXBrqiSrcF6Ee7MQuz8XuAM/yWfRov5-ZkSn3_LbWvrsAvAhhaY\"",
"regionCode": "RU",
"pageInfo": {
"totalResults": 0,
"resultsPerPage": 25
},
"items": []
}
Broke today. Yesterday everything worked correctly.

Json object not escaped properly in bash [duplicate]

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

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