Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Submit form to custom rest (POST)

Neta Elyakim
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 4, 2018

Hey,

I'm trying to submit an HTML form in JIRA dialog.

import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.transform.BaseScript

import javax.ws.rs.core.MediaType
import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.Response

@BaseScript CustomEndpointDelegate delegate

showDialog(){ MultivaluedMap queryParams ->

    // get a reference to the current page...     def issuekey = queryParams.getFirst("issuekey")


    def dialog =
        """<section role="dialog" id="sr-dialog" class="aui-layer aui-dialog2 aui-dialog2-medium" aria-hidden="true" data-aui-remove-on-hide="true">
            <header class="aui-dialog2-header">
                <h2 class="aui-dialog2-header-main">Send comment</h2>
                <a class="aui-dialog2-header-close">
                    <span class="aui-icon aui-icon-small aui-iconfont-close-dialog">Close</span>
                </a>
            </header>
            <div class="aui-dialog2-content">
                <p>
                <form action="http://localhost:8080/rest/scriptrunner/latest/custom/sendcomment" method="post" id="usrform" class="aui" enctype='application/json'>                 <fieldset>
                <textarea class="textarea" name="comment" id="textarea-id" placeholder="Your comment here..."></textarea>
                </fieldset>
                <input id='issuekey' type="hidden" name="issuekey" value='"""+issuekey+"""'>
                <div class="buttons">
                <input class="button submit" type="submit" value="Submit" id="comment-save-button">
                </div>
                </form>
                </p>
            </div>
            <footer class="aui-dialog2-footer">
                <div class="aui-dialog2-footer-actions">
                    <button id="dialog-close-button" class="aui-button aui-button-link">Close</button>
                </div>
                <div class="aui-dialog2-footer-hint"></div>
            </footer>
        </section>
        """

    Response.ok().type(MediaType.TEXT_HTML).entity(dialog.toString()).build()
}

 

The problem is that I get "XSRF check failed" error.

I even tried to just print a log from the custom rest but I it does not print to the logs.

I get this is the log error:

2018-04-04 11:58:54,477 http-nio-8080-exec-4 WARN admin 718x3452x1 jr3nl4 0:0:0:0:0:0:0:1 /rest/scriptrunner/latest/custom/sendcomment [c.a.p.r.c.security.jersey.XsrfResourceFilter] XSRF checks failed for request: http://localhost:8080/rest/scriptrunner/latest/custom/sendcomment , origin: http://localhost:8080 , referrer: http://localhost:8080/projects/STAR/issues/STAR-1

I saw that the solution it submit the form with JQuery but It's not recommended to add <script> tag to inline groovy code

add this to the post header- "X-Atlassian-Token", "nocheck"

Is anybody know a way to add it to the HTML code?

Thanks

1 answer

Suggest an answer

Log in or Sign up to answer
0 votes
Gonchik Tsymzhitov
Community Champion
September 23, 2018

Hi! 

 

Have you implemented it? https://stackoverflow.com/questions/9516865/how-to-set-a-header-field-on-post-a-form

But you can do it as ajax function 

like this 

$.ajax({    url: 'foo/bar',    headers: { 'X-Atlassian-Token': 'nocheck' }
});

 

Cheers,

Gonchik Tsymzhitov 

TAGS
AUG Leaders

Atlassian Community Events