I've looked into the documentation a bit but I have had mixed results with no direct success. https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Bworkspace%7D/%7Brepo_slug%7D/src#post
I simply want to be able to do a POST call and update or add a file to a branch.The file will be a raw JSON example below.
{ "TestFile": "Test"}
How do I name this file? How can I update an existing file like this. is there any better documentation for something like this?
Thanks in advance!
Hi Cameron and welcome to the community.
If you have a file named e.g. File1.json, and you want to create a commit and add it to the repo at the root directory of the repo, at a branch named e.g. test-branch, you can do it with a curl command like below:
curl -X POST -u username:app-password https://api.bitbucket.org/2.0/repositories/<workspace-id>/<repo-slug>/src -F File1.json=@File1.json -F branch=test-branch -F message="my commit message"
The command needs to be executed at the directory where File1.json exists.
You can name the file however you wish. If you want the file to have a different name in the repo than your local, e.g. File2.json, you can alter the above command as follows:
curl -X POST -u username:app-password https://api.bitbucket.org/2.0/repositories/<workspace-id>/<repo-slug>/src -F File2.json=@File1.json -F branch=test-branch -F message="my commit message"
You can update the file with the same command. If a file exists with the same name in the repo, its contents will get modified.
Are you able to do this with a command like above? If there are any issues, could you add the -v parameter in the command and paste both the command you use and the output here so we can take a look?
Please make sure to remove the line that starts with > Authorization: from the output before you share it, as it includes credential info, and also remove the actual password from the command before you post it here. You may also remove the workspace-id, repo name and username if you don't want them public.
Kind regards,Theodora
Hey Theodora - Thank you for your reply!
When I attempt to use the API you provide with UN/PW or a Bearer token I get 401's Errors. Either saying that my Token has expired or a 401 with no other context.The token is newly created so I don't think that's the issue with that or the credentials. I am using a Company Instance of Bitbucket... does anything change with that usage?
Here's a sample curl.
curl --location --request POST 'https://api.bitbucket.org/2.0/repositories/<REPO>/<SLUG>' \--form 'File1.json=@File1.json' \--form 'branch=<testBranch>' \--form 'message=testCommit'
Here's another one using the API format that worked with our companies bitbucket instance.
curl --location --request POST 'https://bitbucket.es.ad.<domain_Name>.com/rest/api/1.0/projects/<WORKSPACE_ID>/repos/<slug>' \--form 'File1.json={"test":"test}' \--form 'branch=<BRANCH>' \--form 'message=testCommit'
curl --location --request POST <domain_Name>.com/rest/api/1.0/projects/<WORKSPACE_ID>/repos/<slug>' \--form 'File1.json={"test":"test}' \--form 'branch=<BRANCH>' \--form 'message=testCommit'
Hi @Cameron ,
Thank you for the info.
A 401 error indicates an issue with authentication. A few things I suggest that you check (I believe we show a 403 error in these cases instead of 401, but it's worth checking anyway):
I am using a Company Instance of Bitbucket... does anything change with that usage?
Does your company use SAML? Do you get redirected to your company's SSO when you log in to Bitbucket website?
If so, this explains the issue with the password. In this case, the account's password won't work for authentication, and you'll need to use an app-password instead:
When you generate it, make sure that the app-password has at least write access to repositories.
If you don't use SSO for login, I would suggest
For OAuth, can you check if the OAuth consumer has at least write access to repositories?
One additional note, the URL in the curl call should be:
https://api.bitbucket.org/2.0/repositories/<workspace-id>/<repo-slug>/src
The example one you posted has <REPO> instead of <workspace-id> and is missing the /src part in the end, not sure if you accidentally removed it from the URL after copy-pasting it here?
It looks like you're new here. Sign in or register to get started.