REST POST request from Confluence html macro returns “HTTP Status 415 – Unsupported Media Type” erro

mgunduc December 9, 2017

We are (F5 Networks, Inc) heavy Jira and Confluence using company.

I am stuck while attempting to create Jira issues through html macro in Confluence.

I am essentially using <form> element to get some input and send it through a java script making an REST API POST request  (via XMLHttprequest)  with Content-Type set to application/json.

I get “HTTP Status 415 – Unsupported Media Type” error.

However, if I disable <form> element and use <button> element (commented in the code below), it works. 

Some insight on this behaviour would greatly be appreciated.

Here is the code snippet:

<form method="post" action="https://<Server URL>/rest/jira-integration/1.0/issues?applicationId=29bbc237-8c6b-3a19-8e64-4da8fed882e6" name="myForm">

         TMA Owner: <input type="text" name="tmao" id="tmao">

        <input type="submit" value="Submit" onclick="submitform()">

</form>

 <!--

<button type="button" onclick="submitform()">Submit</button>

-->

<script type="text/javascript">

function submitform() {

        var data = {"issues":[{"fields":{"project":{"key":"TMA"}, "issuetype":{"name":"TMA Review"}, "customfield_11683":{"name": "gunduc"}, "summary":"TMA 1", "description":"TMA testing"}}]};

         var xhr = new XMLHttpRequest();

        xhr.open("POST", "https://<Server URL>/rest/jira-integration/1.0/issues?applicationId=29bbc237-8c6b-3a19-8e64-4da8fed882e6", true);

alert("Sending JSON3.1");

        xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');

        xhr.setRequestHeader('Accept', 'application/json);

        xhr.send(JSON.stringify(data));

        console.log(xhr.responseText.substring(0, 150)) ;

};

</script>

1 answer

0 votes
Aleksandr Zuevich
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.
December 10, 2017

Hi,

please compare your requests in two cases (using browser Network console). I suppose when you press Submit in form the data is sent using form-data while when you press Submit outside the form you invoke your submitform function. If you are sure that your function is invoked in two cases check stopPropagation or preventDefault event's methods.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events