How can I modify file and push to my branch by Rest Api v2?

I’m trying to modify some files and push them to the test branch. I didn’t find something in the documentation. can you please anyone tell me how can I do that? and can I push multiple files? (not I’m talking about Bitbucket rest APIv2, not Bitbucket server)

Hello @SuhaibBaba

I just did a Google search of ‘bitbucket rest api push branch’ and the first result returned looks like it is the answer.

@sunnyape I tried this solution

const request = require('request');

const options = {
  url: 'https://api.bitbucket.org/2.0/repositories/{username}/{repo_slug}/src',
  method: 'PUT',
  headers: {
    'Content-Type': 'application/json',
    Authorization: 'Bearer {access_token}',
  },
  body: JSON.stringify({
    message: 'Commit message',
    branch: 'branch-name',
    source: {
      path: 'file.txt',
      content: 'File contents',
    },
  }),
};

request(options, function (error, response, body) {
  if (error) {
    console.log(error);
  } else {
    console.log(body);
  }
});

and it returns This endpoint does not support token-based authentication.
I’m already use token-based authentication

Hello @SuhaibBaba

Where did you get that ‘solution’? In the article linked to that Google result it contains an example that shows that REST API endpoint uses a POST method, not a PUT method.

You also might try referring to the REST API documentation where it says the same:

Create a commit by uploading a file

POST /2.0/repositories/{workspace}/{repo_slug}/src. This endpoint is used to create new commits in the repository by uploading files.

Anyhow, good luck on your learning adventure.

@sunnyape It’s working.
Thanks