Script runnenr: Calling canned CopyTree script
Asked by Koen Gillard (CalmWare):

Dear

I've created a REST Endpoint in which I would like to use the canned CopyTree script.

```
@BaseScript CustomEndpointDelegate delegatecopyPageTree(    httpMethod: "POST", groups: ["jira-users", "confluence-users"]) { MultivaluedMap queryParams, String body -&gt;    def space = new JsonSlurper().parseText(body)    //User defaultUser = userAccessor.getUserByName("koen.gillard")    def copyPageTreeParams = [        source_page: "ABC:ABC+Home",        target_page: "DEF:DEF+Home"    ]    def confluence = new HTTPBuilder(&lt; url &gt;)    confluence.client.addRequestInterceptor(new HttpRequestInterceptor() {        void process(HttpRequest httpRequest, HttpContext httpContext) {            httpRequest.addHeader('Authorization', 'Basic &lt;...&gt;')        }    })    confluence.request(POST, JSON) { req -&gt;        uri.path = "/rest/scriptrunner/latest/canned/com.onresolve.scriptrunner.canned.common.admin.CopyTree"        send URLENC, [scriptParams: new JsonBuilder(copyPageTreeParams)]        response.success = { resp, xml -&gt;            println "CopyTree response status: ${resp.statusLine}"            assert resp.statusLine.statusCode == 200        }    }    return Response.ok(new JsonBuilder([space: space]).toString()).build()}
```

When I take a look at the log it states these warning and errors:

```
2016-08-08 15:05:37,835 ERROR [http-nio-8090-exec-17] [runner.rest.common.UserCustomScriptEndpoint] doEndpoint ************************************************************************************* -- url: /rest/scriptrunner/latest/custom/copyPageTree | traceId: 3d0de25e60a38050 | userName: koen.gillard | sr.execution.id: 3001bb7e-e8b4-4d0c-b02a-f8af2879d9602016-08-08 15:05:37,837 ERROR [http-nio-8090-exec-17] [runner.rest.common.UserCustomScriptEndpoint] doEndpoint Script endpoint failed on method: POST copyPageTree -- url: /rest/scriptrunner/latest/custom/copyPageTree | traceId: 3d0de25e60a38050 | userName: koen.gillard | sr.execution.id: 3001bb7e-e8b4-4d0c-b02a-f8af2879d960java.lang.IllegalArgumentException: Text must not be null or empty	at Script586$_run_closure1.doCall(Script586.groovy:33)	at com.onresolve.scriptrunner.runner.rest.common.UserCustomScriptEndpoint.doEndpoint(UserCustomScriptEndpoint.groovy:304)	at com.onresolve.scriptrunner.runner.rest.common.UserCustomScriptEndpoint.postUserEndpoint(UserCustomScriptEndpoint.groovy:208)2016-08-08 15:06:22,652 WARN [http-nio-8090-exec-10] [common.security.jersey.XsrfResourceFilter] passesAllXsrfChecks XSRF checks failed for request: https://confluencet.blogistics.be/rest/scriptrunner/latest/canned/com.onresolve.scriptrunner.canned.common.admin.CopyTree , origin: null , referrer: null -- url: /rest/scriptrunner/latest/canned/com.onresolve.scriptrunner.canned.common.admin.CopyTree | traceId: 0e8783c4e0b4c430 | userName: koen.gillard2016-08-08 15:06:22,662 ERROR [http-nio-8090-exec-1] [runner.rest.common.UserCustomScriptEndpoint] doEndpoint ************************************************************************************* -- url: /rest/scriptrunner/latest/custom/copyPageTree | traceId: 678498cee849dab8 | userName: koen.gillard | sr.execution.id: 41121a63-6e96-405b-b680-caeb8150c53e2016-08-08 15:06:22,664 ERROR [http-nio-8090-exec-1] [runner.rest.common.UserCustomScriptEndpoint] doEndpoint Script endpoint failed on method: POST copyPageTree -- url: /rest/scriptrunner/latest/custom/copyPageTree | traceId: 678498cee849dab8 | userName: koen.gillard | sr.execution.id: 41121a63-6e96-405b-b680-caeb8150c53egroovyx.net.http.HttpResponseException: Forbidden	at groovyx.net.http.HTTPBuilder.defaultFailureHandler(HTTPBuilder.java:651)	at groovyx.net.http.HTTPBuilder$1.handleResponse(HTTPBuilder.java:503)	at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:222)	at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:164)	at groovyx.net.http.HTTPBuilder.doRequest(HTTPBuilder.java:515)	at groovyx.net.http.HTTPBuilder.doRequest(HTTPBuilder.java:434)	at groovyx.net.http.HTTPBuilder.request(HTTPBuilder.java:383)	at groovyx.net.http.HTTPBuilder$request.call(Unknown Source)	at Script586$_run_closure1.doCall(Script586.groovy:47)	at com.onresolve.scriptrunner.runner.rest.common.UserCustomScriptEndpoint.doEndpoint(UserCustomScriptEndpoint.groovy:304)	at com.onresolve.scriptrunner.runner.rest.common.UserCustomScriptEndpoint.postUserEndpoint(UserCustomScriptEndpoint.groovy:208)
```

Any suggestions on how to solve this?

I know the BASIC \<infohere\> is correct because I'm using this to call the Custom REST Endpoint.

Kind regards

Koen Gillard


Answers:
author: JamieA
body: There are multiple problems here, principally the URL is wrong, and the parameters are wrong in name and type.

How can you know what they are? By using the network tools to examine the request in your browser when you do it via the UI, or using something like Fiddler.

Below is a working script that calls copy tree remotely. I presume the caller is not the same instance as the target host, if it is there are simpler ways to do this.

```
import groovy.json.JsonBuilderimport groovyx.net.http.ContentTypeimport groovyx.net.http.HTTPBuilderimport groovyx.net.http.Methodimport org.apache.http.HttpRequestimport org.apache.http.HttpRequestInterceptorimport org.apache.http.protocol.HttpContextdef copyPageTreeParams = [    FIELD_SRC_PAGE_ID   : "[\"98310\"]",    FIELD_TARGET_PAGE_ID: "[\"753700\"]",]def confluence = new HTTPBuilder("http://localhost:8080/confluence/")confluence.client.addRequestInterceptor(new HttpRequestInterceptor() {    void process(HttpRequest httpRequest, HttpContext httpContext) {        httpRequest.addHeader('Authorization', 'Basic ' + 'admin:admin'.bytes.encodeBase64().toString())        httpRequest.addHeader('X-Atlassian-Token', 'no-check')    }})confluence.request(Method.POST, ContentType.JSON) { req -&gt;    uri.path = "rest/scriptrunner/latest/canned/com.onresolve.scriptrunner.canned.confluence.admin.CopyTree"    send ContentType.URLENC, [scriptParams: new JsonBuilder(copyPageTreeParams)]    response.success = { resp, xml -&gt;        println "CopyTree response status: ${resp.statusLine}"        assert resp.statusLine.statusCode == 200    }}
```

comments: author: Koen Gillard (CalmWare)
body: Thanks Jamie!

I indeed want to use the script from 'inside' the same Confluence instance.

You mention a simpler way, are you willing to share this solution?


author: JamieA
body: Please could you use the \\{code\\} macro, which I have done for you now - code is unreadable without the formatting.

Additionally including the imports makes it much easier for the person trying to help reproduce the issue.


Tags: addon-com.onresolve.confluence.groovy.groovyrunner

scriptrunner