1

I want to create a script with helping of this document but I don't know how to pass a groovy file instead of json and when I execute

curl -u admin:admin123 -X POST --header 'Content-Type: application/json' \                                                                                                                                                                ─╯
 http://<url>/service/rest/v1/script \
 -d create_task.groovy

I got this error:

{ "id" : "*", "message" : "Could not process the input: Unrecognized token 'create_task': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')" }%

Any help would be appreciated!

Jessica
  • 127
  • 2
  • 10

1 Answers1

0

enter code hereI found the solution! you have to first convert that groovy script to a JSON file and pass that JSON file to your curl or API(for creating script) that you are working with and we can do it with this python file:

import json

with open("[THE GROOVY FILE]", "r") as inputfile:
    filedata = inputfile.read()
    jsondata = {}
    jsondata['name'] = 'testscript2'
    jsondata['type'] = 'groovy'
    jsondata['content'] = filedata

    with open("[OUTPUT FILE]", "w") as outputfile:
        outputfile.write(json.dumps(jsondata))

the output file is something like this:

{
"name": "name",
"type": "groovy",
"content": "..."
}

then use this output file which is a JSON file and pass this to that command and there you go! the command for creat script with REST API is:

curl -v -X POST -u admin:admin --header "Content-Type: application/json" "http://<url>/service/rest/v1/script" -d @{output}.json

the above command will create a script and you can execute it with:

curl -v -X POST -u admin:admin --header "Content-Type: application/json" "http://<url>/service/rest/v1/script/{name}/run
Jessica
  • 127
  • 2
  • 10