curl custom slack notifications from jenkins - json

I am trying to send an custom slack notification to my slack channel using curl. Below my payload . Using curl to post notifications to Slack channel so that Team members could be able to see the Verison, s3 link and Directly they can access directly. Any suggestions or inputs would be greatly appreciated.
version=1.2.4
bundleversion=1.3.4.5
SLACK_MSG="Version=$version bundleversion=$bundleversion s3link:Random "
curl -H "Content-type: application/json" -X POST --data-urlencode -d
"payload='{
"username": "Kalyan",
"attachments": [
{
"color": "danger",
"fields": [
{
"title": "Danger Event",
"text": "$SLACK_MSG",
"short": false
}
]
},
{
"color": "warning",
"fields": [
{
"title": "Warning Event",
"value": "This is a warning",
"short": false
}
]
},
{
"color": "good",
"fields": [
{
"title": "Good Event",
"value": "This is good",
"short": false
}
]
}
]
}'" https://hooks.slack.com/services/XXXXXX/XXXXXXX/XXXXXXXXXX
Below Jenkins Error
curl: (3) [globbing] nested brace in column 51
curl: (3) Illegal characters found in URL
curl: (6) Could not resolve host: bundleversion=1.3.4.5
curl: (3) Port number ended with 'R'
curl: (3) [globbing] unmatched close brace/bracket in column 52
curl: (3) Illegal characters found in URL
curl: (6) Could not resolve host: is
curl: (6) Could not resolve host: a
curl: (3) Illegal characters found in URL
curl: (6) Could not resolve host: is
curl: (3) [globbing] unmatched close brace/bracket in column 56
Any inputs Greatly Appreciated.

Your first danger attachment has a field with an invalid property called text specified - change this property to value to make it into a valid Slack field.
You can troubleshoot Slack message payloads via the Slack Message Formatting page to see if they are valid: here's the corrected message payload.

I will also make it simple:
Please note: There should not be any space after "content-type:" and "application/json" people do that mistake.
and take care about backslash. You can also try this on terminal/cmd. I should work
curl -X POST -H "Content-type:application/json" --data '{\"text\":\"here_is_your_message\"}' YOUR_WEBHOOK_URL

try this: payload and curl for custom slack notification.
I used that in Gitlab-CI.yml file for sending Job artifacts and reports to Slack. It works fine.
The code below you can modify because I wrote that like it appears Green/Red when job Pass or Fail.
- 'curl -H "Content-Type:application/json" -X POST --data "{
\"attachments\": [
{
\"mrkdwn_in\": [\"text\"],
\"color\": \"#36a64f\",
\"author_name\": \"<https://$Gitlab_Home_URL/${GITLAB_USER_LOGIN}|${GITLAB_USER_NAME}>($GITLAB_USER_LOGIN)\",
\"text\": \"*Job <https://$REPO_URL/-/jobs/${CI_JOB_ID}|TESTING> was SUCCESSFUL in pipeline <https://$REPO_URL/pipelines/${CI_PIPELINE_ID}|${CI_PIPELINE_ID}>*\",
\"fields\": [
{
\"title\": \"Trigger source\",
\"value\": \"$CI_PIPELINE_SOURCE\",
\"short\": true
},
{
\"title\": \"Branch\",
\"value\": \"<https://$REPO_URL/tree/$CI_COMMIT_REF_NAME|$CI_COMMIT_REF_NAME>\",
\"short\": true
},
{
\"title\": \"Commit message\",
\"value\": \"<https://$REPO_URL/commit/${CI_COMMIT_SHA}|$CI_COMMIT_TITLE>\",
\"short\": true
}
],
\"footer\": \"<https://$REPO_URL|$CI_PROJECT_NAME>\",
\"footer_icon\": \"https://www.stickpng.com/assets/images/5847f997cef1014c0b5e48c1.png\",
}
]
}" YOUR_SLACK_WEBHOOK'

Related

Pass JSON as a value to a key to trigger gitlab pipeline

I am trying to trigger a gitlab pipeline with the below payload. I am passing JSON as a value to a predefined variable (wwww) .
{
"ref": "test_branch",
"variables": [
{"key": "xxx", "value": "abc"},
{"key": "yyyy", "value": "def"},
{"key": "zzzz", "value": "ghi"},
{"key": "wwww", "value": "{"test1" : "test2"}"}
]
}
If I trigger the gitlab pipeline with the above payload it fails with below error
curl --request POST --header PRIVATE-TOKEN: xxxxxxxxx --header Content-Type: application/json --data #sample.json https://gitlab.example.com/api/v4/projects/12345/pipeline
curl: (6) Could not resolve host: xxxxxxxxx
curl: (6) Could not resolve host: application
{"error":"The provided content-type '' is not supported."}
I know the above payload is incorrect. Can someone help with the correct method to pass JSON to a key

Can't POST json data with curl in windows

I have read several posts about this topic but i couldn't figure out how to send json data with curl POST on windows 10 (powershell).
I tried with \" or with """ nothing.
json data:
{
"frames": [
{
"text": "HOME2",
"icon": "i294",
"index": 0
},
{
"text": "? 65",
"icon": null,
"index": 1
}
]
}
Example of curl tries:
> curl -H "Accept: application/json" -H "X-Access-Token: xyz" -X POST "https://xyz" -d "{ """frames""": [{ """text""": """HOME2""", """icon""": """i294""", """index""": 0 }, { """text""": """? 65""", """icon""": null, """index""": 1 }]}"
{"error":{"code":null,"message":"Bad Request","trace":["request body must not be empty"]}}curl: (3) [globbing] bad range specification in column 2
curl: (6) Could not resolve host: text
curl: (6) Could not resolve host: HOME2,
curl: (6) Could not resolve host: icon
curl: (6) Could not resolve host: i294,
curl: (6) Could not resolve host: index
curl: (6) Could not resolve host: 0
curl: (3) [globbing] unmatched close brace/bracket in column 1
curl: (3) [globbing] unmatched brace in column 1
curl: (6) Could not resolve host: text
curl: (6) Could not resolve host: ? 65,
curl: (6) Could not resolve host: icon
curl: (6) Could not resolve host: null,
curl: (6) Could not resolve host: index
curl: (6) Could not resolve host: 1
curl: (3) [globbing] unmatched close brace/bracket in column 1
with \"
> curl -H "Accept: application/json" -H "X-Access-Token: xyz" -X POST "https://xyz" -d "{ \"frames\": [ { \"text\": \"HOME2\", \"icon\": \"i294\", \"index\": 0 }, { \"text\": \"? 65\", \"icon\": null, \"index\": 1 } ] }"
{"error":{"code":null,"message":"Bad Request","trace":["request body must not be empty"]}}curl: (3) [globbing] bad range specification in column 2
curl: (3) [globbing] unmatched brace in column 1
curl: (6) Could not resolve host: \text\
curl: (6) Could not resolve host: \HOME2\,
curl: (6) Could not resolve host: \icon\
curl: (6) Could not resolve host: \i294\,
curl: (6) Could not resolve host: \index\
curl: (6) Could not resolve host: 0
curl: (3) [globbing] unmatched close brace/bracket in column 1
curl: (3) [globbing] unmatched brace in column 1
curl: (6) Could not resolve host: \text\
curl: (6) Could not resolve host: \? 65\,
curl: (6) Could not resolve host: \icon\
curl: (6) Could not resolve host: null,
curl: (6) Could not resolve host: \index\
curl: (6) Could not resolve host: 1
curl: (3) [globbing] unmatched close brace/bracket in column 1
curl: (3) [globbing] unmatched close brace/bracket in column 1
curl: (3) [globbing] unmatched close brace/bracket in column 1
What am i doing wrong?
So, after further reading and from the answer from mohsen, I finalized my command line to:
curl -H "Accept: application/json" -H "X-Access-Token: xyz" -X POST "https://xyz" -d #exported.json
I added the json into a file called exported.json and now it is more compact and it still works.
The json data it self didn't need much more tweaking, see exported.json file:
{"frames":[{"text":"HOME2","icon":"i294","index":"0"},{"text":"? 65","icon":"null","index":"1"}]}
first minify your json here, don't use pretty json.
try this:
curl -d 'curl -d '{"frames":[{"text":"HOME2","icon":"i294","index":0},
{"text":"? 65","icon":null,"index":1}]}' -H 'Content-Type: application/json'
https://example.com/login
for more info check this site
Looks like Windows command line and PowerShell messes with curl and JSON post data. Tried the following in CMD, PowerShell and WSL. WSL gives the result I expected, the other two fails. (originally tried to post data to Node-Red, and their official example failed, that was my WTF moment)
Try this (httpbin.org just echoes your data back this case):
curl -X POST -d '{"name":"Nick","a":32}' -H "Content-type:application/json" https://httpbin.org/anything
Response when called from WSL:
{
"args": {},
"data": "{\"name\":\"Nick\",\"a\":32}",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Content-Length": "22",
"Content-Type": "application/json",
"Host": "httpbin.org",
"User-Agent": "curl/7.71.1"
},
"json": {
"a": 32,
"name": "Nick"
},
"method": "POST",
"origin": "*********",
"url": "https://httpbin.org/anything"
}
Response when called from CMD / PS:
{
"args": {},
"data": "'{name:Nick,a:32}'",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Content-Length": "18",
"Content-Type": "application/json",
"Host": "httpbin.org",
"User-Agent": "curl/7.55.1"
},
"json": null,
"method": "POST",
"origin": "****",
"url": "https://httpbin.org/anything"
}
Notice Content-Length is different and the json property in the response is null when called from cmd or ps.
But doing this does work from both WSL and cmd, but not from PowerShell: curl -d "{\"name\":\"Nick\",\"a\":32}" -H "Content-type:application/json" https://httpbin.org/anything
And this works in PowerShell but fails in the other two curl -d '{\"name\":\"Nick\",\"a\":32}' -H "Content-type:application/json" https://httpbin.org/anything
So yes, put your post data in a file. But of course, powershell has it's own rules, so in the others you can do -d #data.json, but in powershell you must fo -d `#data.json (backtick before the # symbol)

Cocatenate 2 variables to put inside json is not working

I need to concatenate 2 variables, but when i execute the bash code i get this error filters.api.malformed_request_body.
I need to get the current IPs, to be able to add a new IP to the filter, that's because i need to use two variables, $a is the current IP that is in my firewall rule, and $b is the new IP that i will add.
From Cloudflare
To preserve existing values, issue a GET request and based on the response, determine which fields (and respective values) to include in your PUT request and that way, avoid any undesired overwrites.
Code:
a=122.16.89.10
b=137.77.77.77
curl -X PUT \
-H "X-Auth-Email: EMAIL" \
-H "X-Auth-Key: KEY" \
-H "Content-Type: application/json" \
-d '[
{
"id": "ID",
"paused": false,
"expression": "(ip.src in {'$a'" "'$b'})",
"description": "Block IP"
}
]' "https://api.cloudflare.com/client/v4/zones/ZONE/filters"
I also tried: "(ip.src in {'$a $b'})" and:
new_filter="$a $b"
...
...
"(ip.src in {'$new_filter'})"
If i echo $new_filter it shows the correct result:
new_filter="$a $b"
echo $new_filter
#122.16.89.10 137.77.77.77
When i use the variable $new_filter it also show this error curl: (3) [globbing] unmatched close brace/bracket in column 13 line 13 is this one -H "Content-Type: application/json" \.
None worked, why? I get this error:
{
"result": null,
"success": false,
"errors": [
{
"code": 10014,
"message": "filters.api.malformed_request_body"
}
],
"messages": []
}
This works: "(ip.src in {'$a'})".
Well, let's take your first example and modify it to print the JSON body:
export a=122.16.89.10
export b=137.77.77.77
echo '[
{
"id": "ID",
"paused": false,
"expression": "(ip.src in {'$a'" "'$b'})",
"description": "Block IP"
}
]'
The output is this:
{
"id": "ID",
"paused": false,
"expression": "(ip.src in {122.16.89.10" "137.77.77.77})",
"description": "Block IP"
}
]
You can see that JSON is not valid. The double quotes are unbalanced in expression.
Try "expression": "(ip.src in {'$a' '$b'})", instead -- that will produce valid JSON.

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.