In order to upload a groovy script via the REST API, the script must be encoded within a JSON payload.
The following Python script can be used as a starting point to achieve this:
import json
with open("testscript.groovy", "r") as inputfile:
filedata = inputfile.read()
jsondata = {}
jsondata['name'] = 'testscript'
jsondata['type'] = 'groovy'
jsondata['content'] = filedata
with open("test.json", "w") as outputfile:
outputfile.write(json.dumps(jsondata))
(Change testscript.groovy to be the source script file and test.json to be the required output file)
This will produce an output file with the correct payload to be used directly within the REST API call. The script will be called testscript within the subsequent API calls, but this can also be amended as necessary within the script or payload.