问题
I am trying to make a CURL command to hit an elasticsearch endpoint and what I have to do is essentially read a script from a file (a multi-lined file) and then pass that script into the json payload for a POST request. I don't know ho to pass it in correctly to the payload.
I have tried ${script}, "'"${script}"'". Not sure how to proceed next
My script:
int editScore(def x) {
x + 1;
}
ctx.new_post_score = editScore(ctx.post_score);
ctx.new_grade = ctx.new_post_score;
My bash script:
location=C:/Users/Danny/Desktop/script
script=$(<$location)
curl -X PUT "localhost:9200/_ingest/pipeline/testpipeline" -H 'Content-Type: application/json' -d'
{
"description" : "testpipeline",
"processors" : [
{
"script": {
"lang": "painless",
"inline": "'"${script}"'"
}
}
]
}
'
The error I get is:
{"error":{"root_cause":[{"type":"parse_exception","reason":"Failed to parse content to map"}],"type":"parse_exception","reason":"Failed to parse content to map","caused_by":{"type":"json_parse_exception","reason":"Illegal unquoted character ((CTRL-CHAR, code 13)): has to be escaped using backslash to be included in string value\n at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@2b6a9d8e; line: 8, column: 55]"}},"status":400}
回答1:
I would try using the jq command to convert the script content to a json string
location=C:/Users/Danny/Desktop/script
script=$(jq -Rs '.' $location)
curl -X PUT "localhost:9200/_ingest/pipeline/testpipeline" -H 'Content-Type: application/json' -d'
{
"description" : "testpipeline",
"processors" : [
{
"script": {
"lang": "painless",
"inline": '$script'
}
}
]
}
'
来源:https://stackoverflow.com/questions/57797064/how-to-pass-a-variable-that-contains-a-file-to-a-curl-post-payload