如何将包含文件的变量传递给curl POST有效负载? [英] How to pass a variable that contains a file to a curl POST payload?

查看:52
本文介绍了如何将包含文件的变量传递给curl POST有效负载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试执行CURL命令以击中Elasticsearch端点,而我要做的实际上是从文件(多行文件)中读取脚本,然后将该脚本传递给JSON有效负载以进行POST请求.我不知道ho如何将其正确传递给有效负载.

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.

我尝试了$ {script},'" $ {script}'".不确定下一步如何进行

I have tried ${script}, "'"${script}"'". Not sure how to proceed next

我的脚本:

int editScore(def x) {
    x + 1;
}

ctx.new_post_score = editScore(ctx.post_score);
ctx.new_grade = ctx.new_post_score;

我的bash脚本:

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

我得到的错误是:

{"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}

推荐答案

我会尝试使用jq命令将脚本内容转换为json字符串

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

这篇关于如何将包含文件的变量传递给curl POST有效负载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆