Can't POST json data with curl in windows - json

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)

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

curl: (3) [globbing] unmatched close brace/bracket

I tried to use curl to POST a JSON object to ElasticSearch server but keep getting globbing error
This is my curl command:
curl -X POST "localhost:9200/school/_doc/10?pretty" -H "Content-type:application/json" -d "{"firstName":"Bilbo","lastName":"Baggins"}"
And the error I get from the server :
{
"error" : {
"root_cause" : [
{
"type" : "mapper_parsing_exception",
"reason" : "failed to parse"
}
],
"type" : "mapper_parsing_exception",
"reason" : "failed to parse",
"caused_by" : {
"type" : "json_e_o_f_exception",
"reason" : "Unexpected end-of-input: expected close marker for Object (start marker at [Source: (byte[])\"{\"; line: 1, column: 1])\n at [Source: (byte[])\"{\"; line: 1, column: 2]"
}
},
"status" : 400
}
curl: (3) [globbing] unmatched close brace/bracket in column 33
You have a syntax issue with your curl command, proper curl command of your request is
curl -v -XPOST -H "Content-type: application/json" -d '{"firstName":"Bilbo","lastName":"Baggins"}' 'localhost:9200/school/_doc/10?pretty'
Best way to use the Elasticsearch using REST format and rest client like postman but still if you want to use the curl, you can use this online curl builder to avoid the syntax issues.

curl: (3) [globbing] unmatched brace at pos 2

I keep receiving this error : curl: (3) [globbing] unmatched brace at pos 2
and I don't even understand where is "pos 2"
Although strangely I don't have the same problem when I do that from bash. I run that code in Jenkins pipeline. That is why you see sh '''' wrapper
sh '''
curl -u ${GIT_USERNAME}:${GIT_PASSWORD} -H "Content-Type: application/json" -X POST https://tools.company.my.com/bitbucket/rest/build-status/1.0/commits/$GIT_COMMIT --data-binary #- <<BODY \
{
"state": "SUCCESSFUL",
"key": "$JOB_BASE_NAME",
"name": "$BUILD_TAG",
"url": "$BUILD_URL",
"description": "change"
}
BODY
'''
Assuming there is no single quote in ${GIT_PASSWORD} :
sh -c "curl -u '${GIT_USERNAME}:${GIT_PASSWORD}'\
-H 'Content-Type: application/json'\
-X POST 'https://tools.company.my.com/bitbucket/rest/build-status/1.0/commits/$GIT_COMMIT'\
--data-binary #-" << BODY
{
"state": "SUCCESSFUL",
"key": "$JOB_BASE_NAME",
"name": "$BUILD_TAG",
"url": "$BUILD_URL",
"description": "change"
}
BODY

curl custom slack notifications from jenkins

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'

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"
}
]'