Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Deleted user
Level
0 / 0 points
Next:
badges earned

Your Points Tracker
Challenges
Leaderboard
  • Global
  • Feed

Badge for your thoughts?

You're enrolled in our new beta rewards program. Join our group to get the inside scoop and share your feedback.

Join group
Recognition
Give the gift of kudos
You have 0 kudos available to give
Who do you want to recognize?
Why do you want to recognize them?
Kudos
Great job appreciating your peers!
Check back soon to give more kudos.

Past Kudos Given
No kudos given
You haven't given any kudos yet. Share the love above and you'll see it here.

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Retrieving a file from bitbucket REST API v2.0 ?

I have a powershell script which grabs a file from one of our repositories via the REST 1.0 API but I see that's being depreciated and retired soon(ish). 

I'm trying to update my code to grab the file using the 2.0 REST API but having a lot of trouble.

Before trying to write code I'm simply trying to get a curl request to work. I'm using the following: 

curl --user myuser@mydomain.com:redacted "https://api.bitbucket.org/2.0/repositories/MyCompany/myrepo/downloads/Scripts/Environment Setup/test.txt"

Enter host password for user 'myusername@mydomain.com': redacted

{"type": "error", "error": {"message": "Resource not found", "detail": "There is no API hosted at this URL.\n\nFor information about our API's, please refer to the documentation at: https://developer.atlassian.com/bitbucket/api/2/reference/"}}

 

Why am I getting resource not found? Is download the right API call to download a file or should I be using something else?

 

Thanks,
Brad

3 answers

1 accepted

1 vote
Answer accepted

Actually I just realized that "downloads"  isn't what I want. Instead I needed to use:

curl --user myuser@mydomain.com:redacted "https://api.bitbucket.org/2.0/repositories/mycompany/myrepo/src/master/path/to/file.txt"


That works with curl but not with powershell's Invoke-RestAPI. I get a forbidden:

$filepath = "myrepo/src/master/path/to/file"

$uri = "https://api.bitbucket.org/2.0/repositories/MyCompany/$($filepath)"

# Get the files from bitbucket (GIT)
Invoke-RestMethod -Credential $cred -Uri $uri -Proxy $proxyUri -OutFile $destination

----


Exception : System.Net.WebException: The remote server returned an error: (403) Forbidden.

Seems the new bit bucket API expects credentials to be send in the request - and Invoke-RestMethod sends them after its prompted to causing an immediate 403 forbidden. The 1.0 api doesn't have this issue for whatever reason. 

The (ugly) solution is:

$cred = Get-Credential


$uri = "https://api.bitbucket.org/2.0/repositories/MyCompany/$($filepath)"
$username = ($cred.GetNetworkCredential()).username
$password = ($cred.GetNetworkCredential()).password
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username,$password)))

# Get the files from bitbucket (GIT)
Invoke-RestMethod -Headers @{Authorization=$("Basic {0}" -f $base64AuthInfo)} -Uri $uri -Proxy $proxyUri -OutFile $destination
Like Corwin Myers likes this

This is only working for master or ci branch, unable to read a file from feature branch

I had the same issue, to read from a particular file, instead of adding the branch name on the url, send a post request for the branch using -F branch=<your-branch>

@Hbandhu I am having the same issue with a feature branch. It may be that we need to look up the SHA of the HEAD of the feature branch and use .../src/SHA/path/to/file.txt

... although I am finding that prospect not very enticing.

Suggest an answer

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

Calling any interview participants for Bitbucket Data Center

Hi everyone,  We are looking to learn more about development teams’ workflows and pain points, especially around DevOps, integrations, administration, scale, security, and the related challeng...

478 views 5 4
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