2016-12-02 103 views
2

Here它解释了如何使用其REST API与Gremlin服务器进行交互。使用以下命令,我执行相当简单的100-1脚本。将groovy脚本作为文件发送到Gremlin服务器REST API

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

我想是不是使用一个内嵌的脚本,在说,script.groovy定义它。

我可以把它用于这种情况下定义与整个脚本变量工作:

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

但只要我开始超越单行该命令符:

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

回答

3

我创建了一个名为send.groovy的文件,其中包含:

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

然后我通过cur发送它l在:

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

如果你想在脚本本身多行,那么你有编码的内容,使之保持有效的JSON(即将换行符更改为“\ n”,因为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"} 
相关问题