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

Modifying the Rest OAuth example to include a JSON body in the request?

Deleted user April 27, 2012

I managed to get the example provided here to work.

https://developer.atlassian.com/display/JIRADEV/JIRA+REST+API+Example+-+OAuth+authentication

But the example does not illustrate how a JSON body should be added to a request. Obviously, this is necessary in order to perform most POST operations such as creating an issue.

I tried to figure this out by myself but no luck so far. The problem is the OAuthClient class in the library used in the example does not seem to support adding a body to the request.

Any help would be appreciated.

1 answer

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

3 votes
Answer accepted
Deleted user April 29, 2012

I overloaded AtlassianOAuthClient.makeAuthenticatedRequest to support request body. Seems to work.

    public String makeAuthenticatedRequest(String url, String accessToken,
            InputStream bodyAsStream, String contentLength) throws Exception
    {
        
        boolean suppressNPE = false;
        //System.out.println("URL: " + url);
        String method = OAuthMessage.POST;
        if(url.matches(".*/issue/.+")){
            //System.out.println("Changing method to PUT...");
            method = OAuthMessage.PUT;
            suppressNPE = true;
        }
        OAuthAccessor accessor = getAccessor();
        OAuthClient client = new OAuthClient(new HttpClient4());
        accessor.accessToken = accessToken;
        OAuthMessage request = accessor.newRequestMessage(method, url, Collections.<Map.Entry<?,?>>emptySet(), bodyAsStream);
        List<Map.Entry<String,String>> headers = request.getHeaders();
        headers.add(new OAuth.Parameter(HttpMessage.CONTENT_TYPE, "application/json"));
        headers.add(new OAuth.Parameter(HttpMessage.CONTENT_LENGTH, contentLength));
        OAuthMessage response = client.invoke(request, ParameterStyle.QUERY_STRING);

        String responseString = "";
        try{
            responseString = response.readBodyAsString();
        }catch(NullPointerException npe){
            if(!suppressNPE){
                throw npe;
            }
        }

        return responseString;
    }


para October 8, 2015

HI Patrick, When i execute makeAuthenticatedRequest method i got Method Not Allowed exception. Please advise, What i need to do further? Caused by: net.oauth.OAuthProblemException: HTTP/1.1 405 Method Not Allowed at net.oauth.client.OAuthResponseMessage.toOAuthProblemException(OAuthResponseMessage.java:83) at net.oauth.client.OAuthClient.invoke(OAuthClient.java:306) at AtlassianOAuthClient.sendRequest Thanks in Advance. Thanks, Para.

para October 9, 2015

Ahh sorry it worked in the Http. how to do HTTPS.

TAGS
AUG Leaders

Atlassian Community Events