简体   繁体   English

将groovy脚本作为文件发送到Gremlin Server REST API

[英]Send groovy script as a file to the Gremlin Server REST API

Here it is explained how I can interact with a Gremlin Server using its REST API. 此处说明了如何使用其REST API与Gremlin Server进行交互。 With the following command I execute the rather simple 100-1 script. 使用以下命令,我将执行非常简单的100-1脚本。

curl -X POST -d "{\"gremlin\":\"100-1\"}" "http://localhost:8182"

What I want is to instead of using an inline script, define it in say, script.groovy . 我想要的不是使用内联脚本,而是在script.groovy定义它。

I can make it work for this case defining a variable with the entire script: 在这种情况下,我可以使用整个脚本定义一个变量:

GROOVY_LOAD_DATA_SCRIPT=$(<script.groovy)
curl -X POST -d "{\"gremlin\":\"${GROOVY_LOAD_DATA_SCRIPT}\"}" "http://localhost:8182"

But as soon as I start moving beyond one-liners that command breaks: 但是,一旦我开始超越单线,该命令就会中断:

{
    "message": "body could not be parsed"
}

I created a file called send.groovy that contains: 我创建了一个名为send.groovy的文件,其中包含:

{
    "gremlin": "x=1+1;x+3"
}

I then send it via curl with: 然后,我通过curl发送它:

$ curl -X POST --data-binary @send.groovy http://localhost:8182/gremlin
{"requestId":"6c0e7f3a-a16c-4fc1-a636-d462dc02b832","status":{"message":"","code":200,"attributes":{}},"result":{"data":[5],"meta":{}}}

If you want multi-line in the script itself, then you have encode the contents so that it remains valid JSON (ie change your line breaks to "\\n" since JSON doesn't allow line-breaks). 如果您希望脚本本身包含多行,那么您已经对内容进行了编码,以使其保持有效的JSON(即,由于JSON不允许换行,请将换行符更改为“ \\ n”)。

Note that you can use tools like Python to turn the file contents into valid JSON: 请注意,您可以使用Python之类的工具将文件内容转换为有效的JSON:

$ cat /tmp/foo
println "Hello " + 'World!'
1+2

$ echo "{\"gremlin\":$(python -c 'import json, sys; print(json.dumps(sys.stdin.read()))' < /tmp/foo)}"
{"gremlin":"println \"Hello \" + 'World!'\n1+2\n"}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 bash 脚本将未序列化和未转义的 HTML 文件数据发送到 API - Send unserialized & unescaped HTML file data to an API with a bash script 如何通过shell脚本调用groovy文件(filename.groovy)或groovy脚本? - How to invoke a groovy file ( filename.groovy ) or groovy script via shell script? 如何通过文本文件传递bash脚本(composer-rest-server)选项的答案? - How to pass answers to bash script (composer-rest-server) options through text file? 使用incron和bash脚本调用REST API? - Call a REST API using incron and bash script? Shell脚本curl REST API-引用问题 - Shell script curl REST API - quoting issues 如何在 Jenkinsfile 等 groovy 界面中将 shell 脚本回显到文件 - How to echo a shell script to a file in groovy interface like Jenkinsfile 需要一个groovy脚本的文本文件中精确匹配的字符串数 - Need count of exact matching string in a text file for a groovy script 如何在 groovy 脚本中打开文件并编辑其内容 - How to open a file and edit its content in groovy script 通过服务器将字符串发送到python脚本 - Send string to python script through server 如何使用 Jenkins 主动选择参数 groovy 脚本在远程服务器中执行 shell 脚本? - How to execute shell script in remote server using Jenkins Active choice parameter groovy script?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM