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

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.

Related

curl for ElasticSearch7.16: why there must be a blank character in request body?

I use curl 7.81.0 (x86_64-pc-win32) in PowerShell 5.1.19041.610 to access Elastic Search 7.16, all run on my Win10 20H2.
This command line is successful:
PS D:> ./curl -X PUT "localhost:9200/customer/_doc/1?pretty" -H "Content-Type:application/json" --data " {\`"name\`":\`"John Doe\`"}"
And get output:
{
"_index" : "customer",
"_type" : "_doc",
"_id" : "1",
"_version" : 17,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
},
"_seq_no" : 35,
"_primary_term" : 13
}
You can notice there is a blank character just after the first double-quotation mark in --data " {
But if I delete that blank character after the first double-quotation mark, just as --data "{, the command line get an error result:
PS D:> ./curl -X PUT "localhost:9200/customer/_doc/1?pretty" -H "Content-Type:application/json" --data "{\`"name\`":\`"John Doe\`"}"
And get output:
{
"error" : {
"root_cause" : [
{
"type" : "mapper_parsing_exception",
"reason" : "failed to parse field [name] of type [text] in document with id '1'. Preview of field's value: ''"
}
],
"type" : "mapper_parsing_exception",
"reason" : "failed to parse field [name] of type [text] in document with id '1'. Preview of field's value: ''",
"caused_by" : {
"type" : "json_e_o_f_exception",
"reason" : "Unexpected end-of-input in VALUE_STRING\n at [Source: (ByteArrayInputStream); line: 1, column: 14]"
}
},
"status" : 400
}
curl: (3) unmatched close brace/bracket in URL position 5:
Doe"}
^
Why this blank character is so important?
I had tried a lot times to get curl command run in Power Shell!
This is issue coming from using PoweShell, no related to CURL or ElasticSearch. Curly braces { and } are special characters in PowerShell, which denote a variable usage and since they are also used for JSON, you have an issue sending JSON through PowerShell.
I do not know the exact syntax, but in one case, PowerShell is trying to match an opening brace to a closing brace. It looks like just adding a space before the brace will somehow escape it, in this case.
https://www.tutorialspoint.com/powershell/powershell_brackets.htm
I recommend using another terminal or a tool like Postman to send these requests, instead of CURL with PowerShell.

how to GET json query results from Windows command terminal

I have a json query that I am trying to pass through the command line
I have tried replacing outer single quotes with double quotes, but still shows me error
curl -XGET "http://localhost:9200/honda/_search?pretty" -H 'Content-Type:
application/json' -d “#{“query”:{"match":{"color":"silver"}}}”
Expected: Documents that matches field:silver
Actual Error: Warning:
Couldn't read data from file "", this makes an empty POST
THEN DISPLAYS MY ALL MY DOCUMENTS:
{
"_index" : "honda",
"_type" : "_doc",
"_id" : "234",
"_score" : 1.0,
"_source" : {
"model" : "Accord EX",
"price" : 28000,
"color" : "red",
"num_doors" : 4,
"weight" : "9000lbs"
}
}.................................
curl: (6) Could not resolve host: application
{"query":{"match":{"color":"silver"}}}"
The filename, directory name, or volume label syntax is incorrect.
Try escaping the characters for quotes.

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

Windows: curl with json data on the command line

I am running the following command in Windows prompt:
curl -XPUT http://127.0.0.1:9200/test-index/test-type/_mapping?pretty=true -d '{"test-type": {"properties": {"name": {"index": "analyzed", "term_vector": "with_positions_offsets", "boost": 1.0, "store": "yes", "type": "string"}}}}'
I get the following error:
{
"error" : "ElasticsearchParseException[Failed to parse content to map]; nested: JsonParseException[Unexpected character (''' (code 39)): expected a
valid value (number, String, array, object, 'true', 'false' or 'null')\n at [Source: org.elasticsearch.common.compress.lzf.LZFCompressedStreamInput#45
4ed1d2; line: 1, column: 2]]; ",
"status" : 400
}
I searched for solutions and found alternatives such as put json data in files, but I cannot use it for some reasons.
Thanks!
Windows's cmd doesn't support strings with single quotes. Use " and escape the inner ones with \".
"I searched for solutions and found alternatives such as put json data in files, but I cannot use it for some reasons"
This should work, with hello.json in temp. The # is requried.
c:\temp>curl -v -X PUT \
--data "#hello.json" \
-H "Content-Type:application/json" \
http://localhost:8080/api/myresource