I've written a REST resource for JIRA 4.4.5 with multiple paths and GET and POST methods. The REST resource works fine when used manually via it's URL, even the POST works.
However, the gadget's AJS.$.ajax() request doesn't call the URL directly but is proxied through /plugins/servlet/gadgets/makeRequest. In FireBug I can see that my data is transmitted as a field "postData" of a application/x-www-form-urlencoded request. But the postData never reaches my resource's POST method. I tried to get the POST body via String, custom class, MultivaluedMap and even with @FormParam, nothing works when used via AJS.$.ajax(). When using a String parameter I get an empty String.
Here's the gadget's ajax:
var json = {"id":"123"}; var group = 'FOO'; AJS.$.ajax({ url : '/rest/jiragroupmanagementrestresource/latest/group/' + encodeURI(group), type: 'POST', data : JSON.stringify(json), cache : false, success: function(response){ alert('Success'); } });
And here's the rest resource:
package com.example.jira.customization.jira.rest; import java.util.List; import javax.ws.rs.Consumes; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import com.atlassian.jira.security.JiraAuthenticationContext; @Path("/group") public class JiraGroupManagementGroupRestResource { private final JiraAuthenticationContext auth; public JiraGroupManagementGroupRestResource(JiraAuthenticationContext auth) { this.auth = auth; System.err.println(auth.getLoggedInUser().getDisplayName()); } @POST @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_FORM_URLENCODED }) @Produces({ MediaType.APPLICATION_JSON }) @Path("/{groupName}") public Response updateProject(@PathParam("groupName") String groupName, String data) { System.err.println("POST request for group '" + groupName + "'"); if (data != null && !data.isEmpty()) { System.err.println("data: " + data); return Response.ok().build(); } else { System.err.println("data is null or empty"); return Response.serverError().build(); } } }
Again: This is working perfectly when called directly via /rest/jiragroupmanagementrestresource/latest/group/FOO
P.S.: When this is solved, how do I get the content-type to be application/json? makeRequest itself uses application/x-www-form-urlencoded but my post data is application/json.
Community moderators have prevented the ability to post new answers.
The gadget.xml was missing the badly documented
<Optional feature="atlassian.util" />
That caused the oAuth trouble.
Additionally the 415 can be resolved with
headers: { "Content-Type" : "application/json" }
Thanks to the devs for finally helping out.
Woot! I'm glad you got it working!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
The first problem seems to be that you haven't specified at baseurl in you ajax setup. This is needed internally for the gadget to decide wether it is a local or remote request as remote requests need to go through a proxy.
AJS.$.ajaxSetup({ baseUrl: "http://localhost:8090/jira" // The base url of your instance }); AJS.$.ajax({ url : '/rest/jiragroupmanagementrestresource/latest/group/FOO", type: 'POST', data : {}, // form data cache : false, success: function(response){ alert('Success'); } });
The second problem is that the jQuery.ajax wrapper we have written around gadgets.io.makeRequest does not support applicaiton/json contentType. It only supports application/x-www-form-urlencoded. I have created a bug for this to be supported. I can't believe it is not already, apologies. If you really need application/json I suggest using gadgets.io.makeRequest directly.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks, gadgets.io.makeRequest JSON POST works in jira gadget from confluence! Voted for AG-1421
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello Joe, Hello Scott.
Thank you for helping.
I specify the baseUrl when creating the gadget. See the PoC project at JSP-135729. I created a _complete_ plugin project, that - in theory - should be working right out of the box, as soon as the oAuth problem is fixed/removed).
Will the contentType problem persist, if we finnaly workaround the oAuth problem, i.e. is makeRequest still needed without oAuth?
rgds
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
Sorry I'm no gadget expert, but I'll try to add some useful info.
It looks like JIRA is forcing the gadget to go through a proxy servlet, even though the gadget is being served locally. I guess this is causing the gadget's request to lose the authentication information from the current user's session, so the gadget needs to support oauth authentication.
This documentation page has some information on how to embed support for the oauth dance in to your gadget.Have you looked at this? https://developer.atlassian.com/display/GADGETS/Using+Authentication+in+your+Gadget
I'll see if I can find a gadgets guru to weigh in here.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Try with the following params:
processData: false,
contentType: "application/json",
This is what fixed it for me when doing POST ajax calls.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Unfortunately that didn't help. I think the "consumer_key_unknown" bug has to be fixed first.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
The oAuth bug is filed as JSP-135732
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Have you tried adding dataType: "json" to your AJAX call, as in this example?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
dataType is only used for the expected response, not for the request's body.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Community moderators have prevented the ability to post new answers.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.