Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,297,485
Community Members
 
Community Events
165
Community Groups

Bitbucket Server API: add a file to a repository

Edited

3 answers

1 accepted

0 votes
Answer accepted

Hello Daavid and welcome to the Community!

From reviewing your request for the cloud API you want to be mirrored over in server there is an API which you can use on server. The similar REST API endpoint you’re wanting to use is as follows; /rest/api/1.0/projects/{projectKey}/repos/{repositorySlug}/browse/{path:.*}.

This endpoint will allow for the following;

The file can be updated or created on a new branch. In this case, the sourceBranch parameter should be provided to identify the starting point for the new branch and the branch parameter identifies the branch to create the new commit on.

I hope this information proves helpful and you’re able to upload/add files to Bitbucket server without issue.

Regards,
Stephen Sifers

Thank you! This is what I needed.

Like Stephen Sifers likes this

@Stephen Sifers i have issue, when i try to use the API you have mentioned above is not working. instead of updating the file content is reading the file. i have added sample curl which i have tried, 



curl --location --request PUT 'http://example.com/rest/api/1.0/projects/PROJECT_1/repos/repo_1/browse/test.json' \
--header 'Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=' \
--form 'content=test changes' \
--form 'message=Updated using file-edit REST API' \
--form 'branch=development'

Please help me to fix this issue

Hello @arun

The answer above is almost 3 years old, and with Bitbucket Server 7.21 being available, it may no longer apply (As the above reference links to 6.4).

With this said, in Bitbucket Server you could attempt the following endpoint and example:

/rest/api/1.0/projects/{projectKey}/repos/{repositorySlug}/browse/{path:.*} 

PUT

This API can also be invoked via a user-centric URL when addressing repositories in personal projects.

Update the content of path, on the given repository and branch.

This resource accepts PUT multipart form data, containing the file in a form-field named content.

An example curl request to update 'README.md' would be:

 curl -X PUT -u username:password -F content=@README.md  -F 'message=Updated using file-edit REST API'
 -F branch=master -F  sourceCommitId=5636641a50b
  http://example.com/rest/api/latest/projects/PROJECT_1/repos/repo_1/browse/README.md
 
  • branch: the branch on which the path should be modified or created
  • content: the full content of the file at path
  • message: the message associated with this change, to be used as the commit message. Or null if the default message should be used.
  • sourceCommitId: the commit ID of the file before it was edited, used to identify if content has changed. Or null if this is a new file

The file can be updated or created on a new branch. In this case, the sourceBranch parameter should be provided to identify the starting point for the new branch and the branch parameter identifies the branch to create the new commit on.

Regards,
Stephen Sifers

@Stephen Sifers yes i have referred the same doc and tried the same as u have mentioned. You can check the sample curl comment in the above comment.But still instead of updating the content in the file  it’s reading the content from the file what we passed in the path. Is there anything I’m doing wrong here or what could be the issue.

thanks ,

Arun s

Hello @arun 

The only issue I may see is your use of the --location since it seems you're using a proxy server. It may not be possible for your to test without the proxy since it's in place for a reason, but do see if you can test without your proxy.

What I see with -L or --location is the following:

When curl follows a redirect and if the request is a POST, it will send the following request with a GET if the HTTP response was 301, 302, or 303. If the response code was any other 3xx code, curl will re-send the following request using the same unmodified method.

You can tell curl to not change POST requests to GET after a 30x response by using the dedicated options for that: --post301, --post302 and --post303.

The method set with --request overrides the method curl would otherwise select to use.

Source: https://curl.se/docs/manpage.html

Regards,
Stephen Sifers

@Stephen Sifers When u try to use the postman also I’m facing this issue and not just curl.

And have you tried the updating the content of the file any time? It’s working for you?

@arun

Interesting, this post is related to a Server version of our product. Can you confirm if you're using a Server or Cloud version of Bitbucket? If server, can you let me know which version you're presently on?

This will help me understand what might be the problem and help me investigate if there is a known issue around this.

Regards,
Stephen Sifers

@Stephen Sifers yes I’m  using bitbucket server only and version is 7.17.4.

@arun 

Could you please let us know the following:

  1. What is the response code and JSON response you get back from the endpoint?
  2. Were you able to test without the Proxy in place?
  3. Is POST responding correctly or similar to PUT?

I am unable to find any similar issues which may be causing this and need to understand more.

Regards,
Stephen Sifers

@Stephen Sifers @Please find below my replay for your questions,

1. I’m getting as 200 response code and the same Json response as GET method

{
"lines": [
{
"text": "{"
},

{
"text": " \"version\": \"1.0.0\""
},
{
"text": "}"
},
],
"start": 0,
"size": 39,
"isLastPage": true,
"limit": 500,
"nextPageStart": null
}

2.I don’t have details of without proxy server but I’m testing the url which will show while accessing the bitbucket on browser

3. For post method also I’m getting the same response as PUT

Thanks ,

Arun S

@Stephen Sifers any updates on this issue? 

Thanks.

Hello @arun 

All signs are pointing to your Bitbucket Server sitting behind a reverse proxy which is converting your POST and PUT calls into a GET. More evidently, the fact you're getting a 200 back as a GET shows that something is interfering with your API call to the endpoints.

Please work with your Admin to test without the proxy and see if the results and consistent. If removing the proxy is not an option for testing, then please work with your admin to ensure the reverse proxy is configured correctly to allow your API Headers to pass through.

Regards,
Stephen Sifers

@Stephen Sifers I have an issue when I send request that time I receive 415 unsupported media type responses. Content-type(application/json). Basically, I have to create the text file and inside the file send a text.

```https://local.com/rest/api/1.0/projects/XXX/repos/XXXX/browse/test.txt ```

Hello Dinkar,

This post is a bit old so the links are referencing an older version, please see /rest/api/1.0/projects/{projectKey}/repos/{repositorySlug}/browse/{path:.*} 

The example curl call there is as follows:

 curl -X PUT -u username:password -F content=@README.md  -F 'message=Updated using file-edit REST API'
 -F branch=master -F  sourceCommitId=5636641a50b
  http://example.com/rest/api/latest/projects/PROJECT_1/repos/repo_1/browse/README.md

Regards,
Stephen Sifers

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Bitbucket

Git push size limits are coming to Bitbucket Cloud starting April 4th, 2022

Beginning on April 4th, we will be implementing push limits. This means that your push cannot be completed if it is over 3.5 GB. If you do attempt to complete a push that is over 3.5 GB, it will fail...

2,214 views 2 9
Read article

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you