Create a repository using REST in Java

I am trying to create a Bitbucket repository using their REST-API. Everything seems to work except setting the "parent" project, where the repository needs to be created in. On this link a cURL example is provided. In the body, the parameter "scm" is set as either "git" or "hg", both being Strings, the parameter "project" seems to be a json object containing a key-value pair. Everything I tried so far did not work (json object, string, etc.)

Question: How can I create a repository IN a specific project?

My code looks the following:

RestTemplate restTemplate = new RestTemplate();
String url = "https://api.bitbucket.org/2.0/repositories/" + tName + "/" + rName;
HttpHeaders headers = new HttpHeaders();headers.set("Authorization", "Basic 1234567890qwert");headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));

MultiValueMap<String, String> project = new LinkedMultiValueMap();project.add("key", "aaaaaaaa"); //the repo should be created in the project aaaaaaaa

MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();postParameters.add("scm", "hg"); //hg or git, does not matterpostParameters.add("project", project); //<-- the api ignores the declared project

HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<MultiValueMap<String, Object>>(body, headers);

ResponseEntity<BitbucketRepository> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, BitbucketRepository.class);

System.out.println("createRepository: " + response);
return response;

1 answer

0 vote

You seem to be sending a multipart/form-data request document here, not a JSON document. As per the curl example, make sure you properly format a json document and send the "Content-Type: application/json" request header.

The JSON document you should be sending as the body of your request should like this:

{
"scm": "hg",
"project": {
"key": "aaaaaaaaa"
}
}

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Piotr Plewa
Published Dec 27, 2017 in Bitbucket

Recipe: Deploying AWS Lambda functions with Bitbucket Pipelines

Bitbucket Pipelines helps me manage and automate a number of serverless deployments to AWS Lambda and this is how I do it. I'm building Node.js Lambda functions using node-lambda&nbsp...

1,747 views 1 5
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you