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

272 views
Skip to first unread message

Gustavo Mafra

unread,
Dec 1, 2016, 11:45:35 PM12/1/16
to Gremlin-users
http://stackoverflow.com/questions/40923202/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. With the following command I execute the rather simple 100-1 script.


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.


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

Daniel Kuppitz

unread,
Dec 2, 2016, 12:01:59 AM12/2/16
to gremli...@googlegroups.com
You can use Python to turn the file contents into a valid JSON string (doesn't have to be Python, but to me this seems to be the easiest way).

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

Cheers,
Daniel


--
You received this message because you are subscribed to the Google Groups "Gremlin-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/f25e98d5-b70f-4996-8a49-17966205678b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Stephen Mallette

unread,
Dec 2, 2016, 7:09:07 AM12/2/16
to Gremlin-users
I'm pretty sure that you can just reference the file in the curl statement itself:

curl -X POST -d @script.groovy "http://localhost:8182"





Daniel Kuppitz

unread,
Dec 2, 2016, 7:55:43 AM12/2/16
to gremli...@googlegroups.com
Ha, I wrote this answer, then I thought "well, he probably tried this and it didn't work because the wrapping {"gremlin":...} was needed".

Cheers,
Daniel

Stephen Mallette

unread,
Dec 2, 2016, 8:45:13 AM12/2/16
to Gremlin-users
yeah - after i answered this here i tried it myself as part of re-answering on SO:


The file will need to valid JSON which doesn't handle line breaks.

Gustavo Mafra

unread,
Dec 2, 2016, 10:49:48 AM12/2/16
to Gremlin-users
Thanks guys, I kind of solved it by adding semicolons to my script and trimming stuff I didn't want from it with tr:

GROOVY_SCRIPT=$(tr -d "\n\r\t"<query.groovy)

That breaks if there are comments in the script though, and probably for other cases that I don't know.
To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-user...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Gremlin-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-user...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Gremlin-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-user...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Gremlin-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-user...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages