Search code examples
javasignpost

how to create issue using bitbucket API and singpost in java


I am trying to create new issue at bibucket, but i don't know how to work with http. I try many things but it stil dosen't work. This is one of my attempts:

URL url = new URL("https://api.bitbucket.org/1.0/repositories/" 
        + accountname + "/" + repo_slug + "/issues/"
        + "?title=test&content=testtest");

HttpsURLConnection request = (HttpsURLConnection) url.openConnection();       
request.setRequestMethod("POST");
consumer.sign(request);
request.connect();

I don't have a problem with GET requests. But here I don't know how to send parametrs and sign the message.

Here is documentation of API https://confluence.atlassian.com/display/BITBUCKET/issues+Resource#issuesResource-POSTanewissue

How to do this properly?


Solution

  • In the end I figured this out. The parametrs isn't part of the URL, but if you use stream, You can't sign it.

    The solution is use Apache HttpComponents library and add parameters like in code below:

        DefaultHttpClient httpclient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost("https://api.bitbucket.org/1.0/repositories/"
                + accountname + "/" + repo_slug + "/issues/");
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("title", "test"));
        nvps.add(new BasicNameValuePair("content", "testtest"));
        httpPost.setEntity(new UrlEncodedFormEntity(nvps));
        consumer.sign(httpPost); 
        HttpResponse response2 = httpclient.execute(httpPost);
    
        try {
            System.out.println(response2.getStatusLine());
            HttpEntity entity2 = response2.getEntity();
            // do something useful with the response body
            // and ensure it is fully consumed
            EntityUtils.consume(entity2);
        } finally {
            httpPost.releaseConnection();
        }
    
    }
    

    But you must use CommonsHttpOAuthConsumer which is in special signpost library for commonshttp.