PUT 409 errorcode

I can successfully add a new file to BitBucket but when I want to update the file I get a 409 ErrorCode.

Im doing a query to get the latest sourceCommitID by:

http://bitbucket.abc.com:7990/rest/api/1.0/projects/TRAN/repos/i18n/commits?limit=1&path=filename.json

Then I do a PUT:

String url = "http://bitbucket.abc.com:7990/rest/api/1.0/projects/TRAN/repos/i18n/browse/filename.json";
String auth = "myUser:myPass";
String encoding = Base64.getEncoder().encodeToString((auth).getBytes(StandardCharsets.UTF_8));
HttpPut httpPut = new HttpPut(url);
httpPut.setHeader("Authorization", "Basic " + encoding);
HttpEntity reqEntity = MultipartEntityBuilder.create()
.addPart("branch", new StringBody("master", ContentType.TEXT_PLAIN))
 .addPart("content", new StringBody(json, ContentType.TEXT_PLAIN))
.addPart("message", new StringBody("TranslateAPI commit", ContentType.TEXT_PLAIN))
.addPart("sourceCommitID", new StringBody(theSourceCommitID, ContentType.TEXT_PLAIN))
.build();
httpPut.setEntity(reqEntity);
response = httpclient.execute(httpPut);
int statusCode = response.getStatusLine().getStatusCode();
            

Can anyone see any errors in this code or give me a hint what can be wrong?

What’s the error message that comes along with the 409 status?

It seems like I get the 409 trying to update a file with no changes.
The “getReasonPhrase()” gives nothing back.
If I update a file with changes I get correct 200 back.
That might be correct behaviour.

To get the error message, you have to read the response entity. Something like this:

String responseString = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);

When you run that, you will find that the error message is something along the lines of:

{"errors":[{"context":null,"message":"'filename.json' could not be created because it already exists. A previous commit ID must be provided when editing an existing file to prevent concurrent modifications.","exceptionName":"com.atlassian.bitbucket.content.FileAlreadyExistsException"}]}

This lead me to find the small typo in the snippet you posted.

.addPart("sourceCommitID", new StringBody(theSourceCommitID, ContentType.TEXT_PLAIN))

“sourceCommitID” should be “sourceCommitId” (note the lower-case ‘d’).

Additionally, what you said about modifying a file with no changes is correct.

It seems like I get the 409 trying to update a file with no changes.

If your content is the same as the existing file, then git has nothing to commit. The error response you will see in that case will be

{"errors":[{"context":null,"message":"The content provided is the same as what already exists. No change was committed.","exceptionName":"com.atlassian.bitbucket.content.FileContentUnmodifiedException"}]}

Hope this helps,
Kristy