Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Commit file using REST API

Greg
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
January 29, 2020

Hi,

I'm building a web based editor (in React) which needs to commit changes to our BitBucket repo. GET requests seem to working perfectly however the code below is returning a 415 error which I think relates to incorrect content/meta type..?

There's no mention of how to specify a meta type in the docs,

https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Bworkspace%7D/%7Brepo_slug%7D/src#post

Is meta type the real problem here? or have I done something else wrong?

Can anyone help?

thanks

const url = new URL(`https://api.bitbucket.org/2.0/repositories/${workspace}/${repo}/src`);

const params = {
    access_token: query.access_token,
    message: encodeURIComponent("commit"),
    branch: branchName,
    [encodeURIComponent(path + '/start.json')]: "helloWorld"
};

Object.entries(params).forEach(([key, value]) => {
    url.searchParams.append(key, value)
});

fetch(url, { method: 'POST' }).then(handleResponse);

 

1 answer

0 votes
Greg
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
February 14, 2020

Here's the fix for anyone searching...

 

const url = new URL(`https://api.bitbucket.org/2.0/repositories/${workspace}${repo}src`);
url.searchParams.append('access_token', token);

const data = new URLSearchParams();
data.append('message', commitMsg);
data.append('branch', branchName);
data.append(path, fileContent);

const options = {
method: 'post',
headers: {
"Content-type": "application/x-www-form-urlencoded; charset=UTF-8"
},
body : data
}

fetch(url, options).then(
handleResponse);

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events