Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

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

rest api to retrieve content of file

I am trying to migrate code that we wrote against gitlab to stash (3.1), one of these is a rest request to get the content as a file.

https://{my-host}/rest/api/1.0/projects/CLOUD/repos/myrepo/browse/package.json

 This return the content as a set of lines: 

{
  "lines": [
   {
      "text": "{"
    },
    {
      "text": "  \"name\": \"my-project\","
    }
  ],
  "start": 0,
  "size": 3,
  "isLastPage": false
}

Is there a way to return the raw contents(text, base64) of the file instead of having text-text lines?

Also i can't tell from the rest api how to create or update just a file. 

2 answers

1 vote
Jeff Thomas Atlassian Team Dec 15, 2015

You may not be able to get the raw contents of the file from the REST API, but it's available if you tack on ?raw to the full URL of the file. For example, hitting https://{my-host}/projects/CLOUD/repos/myrepo/browse/package.json?raw should return the raw contents of that file.

Thanks!  Spent a lot of time trying to use the REST API to get the raw file, until I read this post.

Hi @Sudeep Yadav   the above is downloading package.json. It isnt fetching the content of package.json

@Shikha Priyedarshi - Can you share the details?

Hi Yes. In a groovy script I am trying to get the rawFile package.json in a variable like below:

``` def bitbucketResponse = "https://bitbucket_url/rest/api/1.0/projects/{projects}/repos/{repos}/raw/{path}/package.json".toURL().getText(requestProperties: [Accept: 'application/json', Authorization: \"Basic \${auth}\"])```

 

so when I tried only "https://bitbucket_url/rest/api/1.0/projects/{projects}/repos/{repos}/raw/{path}/package.json" in web browser it downloaded the package.json.

I saw one of your answers which includes the below url :

{Server}/rest/api/1.0/{project}/repos/{repo}/raw/{file_path}?at={version}

 

what is version here ?

@Shikha Priyedarshi - Oh you are trying it from browser.

Yes, that is expected behavior. Try to make this call using the Postman tool or through your groovy script and in that response you should get the correct value.

Note: You will have to use the respective authorization that you use. 

Yes am using this one from groovy : def xyz = https://bitbucket_url/rest/api/1.0/projects/{projects}/repos/{repos}/raw/{path}/package.json".toURL().getText(requestProperties: [Accept: 'application/json', Authorization: \"Basic \${auth}\"])

but it is not returning anything, the same command is getting stored in the  variable  to .

@Shikha Priyedarshi - I don't think this is how you make a call to a url, you are just assigning a value to a variable that is why it doesn't return anything.

Look up how to make call to endpoints in groovy and you will know what to do.

Suggest an answer

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

New improvements to user management in Bitbucket Cloud 👥

Hey Community! We’re willing to wager that quite a few of you not only use Bitbucket, but administer it too. Our team is excited to share that we’ll be releasing improvements throughout this month of...

3,734 views 10 16
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