rest api to retrieve content of file

Asim Malik December 10, 2015

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
Atlassian Team members are employees working across the company in a wide variety of roles.
December 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.

David Maltby November 29, 2017

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

0 votes
Sudeep Yadav January 21, 2021

Just use 'raw' in place of 'browse' and it should work. 

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

Shikha Priyedarshi June 2, 2021

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

Sudeep Yadav June 2, 2021

@Shikha Priyedarshi - Can you share the details?

Shikha Priyedarshi June 2, 2021

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.

Shikha Priyedarshi June 2, 2021

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 ?

Sudeep Yadav June 2, 2021

@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. 

Shikha Priyedarshi June 2, 2021

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 .

Sudeep Yadav June 2, 2021

@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.

Shikha Priyedarshi June 2, 2021

Okay Thank you

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events