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'
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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 <domain_Name>.com/rest/api/1.0/projects/<WORKSPACE_ID>/repos/<slug>' \--form 'File1.json={"test":"test}' \--form 'branch=<BRANCH>' \--form 'message=testCommit'
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
Kind regards,
Theodora
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks for your quick reply Theodora!
I think the SSO issue may be the overall issue here.
I've read the instructions for creating an app password but i don't see the options in my account settings.
"see the screenshot below "
...
Also a quick side note.
The one time I got the Pull-request API to work, the API path had 1.0 instead of 2.0
Is this a versioning issue?
Best Regards :)
- Cameron
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Cameron,
Thank you for the info.
Your screenshot looks like it's from a Bitbucket Server account (in a self-hosted instance). I think I misunderstood your previous comment ("I am using a Company Instance of Bitbucket..."), I thought you meant it was a Cloud instance with a domain claimed by your company.
Just to make things clear, your company is using Bitbucket Server (self-hosted instance, on your own servers) instead of Bitbucket Cloud (hosted in https://bitbucket.org/), is that correct?
If so, then this is indeed the culprit. The two products have differences and they also have a different API. The API endpoint we've been talking about concerns Bitbucket Cloud only.
The documentation for Bitbucket Server API can be found here:
The endpoint that can be used for creating a new commit (either for updating or creating a new file) can be found here and also includes an example:
Could you give this a try and let me know if it works for you?
Apologies for the misunderstanding and the confusion on this.
Kind regards,
Theodora
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You are very welcome! I'm glad we figured this out!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.