Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

escape triple quotes in curl correctly

I have the following curl request

curl -H "Content-Type: application/json" -X POST http://localhost:9200/_reindex\?wait_for_completion\=true -d '{"source": {"index": "analytics-prod-2019.12.30", "size":1000 }, "dest": {"index": "analytics-prod-2019.12"}, "conflicts": "proceed", "script": { "lang": "painless","source: """ctx._source.index = ctx._index; def eventData = ctx._source["event.data"]; if(eventData != null) { eventData.remove("realmDb.size"); eventData.remove("realmDb.format"); eventData.remove("realmDb.contents"); }""" } }' 

but this fails with the following error:

{"error":{"root_cause":[{"type":"x_content_parse_exception","reason":"[1:166] [script] failed to parse object"}],"type":"x_content_parse_exception","reason":"[1:166] [reindex] failed to parse field [script]","caused_by":{"type":"x_content_parse_exception","reason":"[1:166] [script] failed to parse object","caused_by":{"type":"json_parse_exception","reason":"Unexpected character ('\"' (code 34)): was expecting a colon to separate field name and value\n at [Source: org.elasticsearch.common.bytes.BytesReference$MarkSupportingStreamInputWrapper@51c48433; line: 1, column: 177]"}}},"status":400}

if i remove the script field from the request this works just fine:

curl -H "Content-Type: application/json" -X POST http://localhost:9200/_reindex\?wait_for_completion\=true -d '{"source":{"index":"analytics-prod-2019.12.30","size":1000},"dest":{"index":"test-index"},"conflicts":"proceed"}}'

using the kibana UI works fine.

what is the correct way to run this in curl?

like image 850
khinester Avatar asked Oct 28 '25 16:10

khinester


1 Answers

Use a single " to surround your script value and \u0027 to escape in your Painless script.

curl -H "Content-Type: application/json" -X POST http://localhost:9200/_reindex\?wait_for_completion\=true -d '
{
  "source": {
    "index": "analytics-prod-2019.12.30",
    "size": 1000
  },
  "dest": {
    "index": "analytics-prod-2019.12"
  },
  "conflicts": "proceed",
  "script": {
    "lang": "painless",
    "source": "ctx._source.index = ctx._index; def eventData = ctx._source[\u0027event.data\u0027]; if(eventData != null) { eventData.remove(\u0027realmDb.size\u0027); eventData.remove(\u0027realmDb.format\u0027); eventData.remove(\u0027realmDb.contents\u0027);"
  }
}
'

You can also see an example of this here, click on the Copy as cURL link and review the example in that format.

like image 109
zsltg Avatar answered Oct 31 '25 12:10

zsltg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!