Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Gadget with POST request to custom REST resource - body of POST gets lost

Nabil Sayegh
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.
August 6, 2012

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.

6 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

2 votes
Answer accepted
Nabil Sayegh
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.
September 2, 2012

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.

Joe Clark
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
September 4, 2012

Woot! I'm glad you got it working!

2 votes
frother
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
August 30, 2012

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.

Andriy Zhdanov
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.
January 12, 2016

Thanks, gadgets.io.makeRequest JSON POST works in jira gadget from confluence! Voted for AG-1421

1 vote
Nabil Sayegh
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.
August 30, 2012

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

0 votes
Joe Clark
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
August 30, 2012

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.

0 votes
Dennis Kromhout van der Meer
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.
August 21, 2012

Try with the following params:

processData: false,
contentType: "application/json",

This is what fixed it for me when doing POST ajax calls.

Nabil Sayegh
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.
August 21, 2012

Unfortunately that didn't help. I think the "consumer_key_unknown" bug has to be fixed first.

Nabil Sayegh
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.
August 21, 2012

The oAuth bug is filed as JSP-135732

0 votes
Joe Clark
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
August 6, 2012

Have you tried adding dataType: "json" to your AJAX call, as in this example?

Nabil Sayegh
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.
August 6, 2012

dataType is only used for the expected response, not for the request's body.

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

TAGS
AUG Leaders

Atlassian Community Events