Hello! I want to get the size of a specific file in my repository. There's a solution in the documentation: https://developer.atlassian.com/server/bitbucket/rest/v811/api-group-repository/#api-api-latest-projects-projectkey-repos-repositoryslug-browse-path-get
However, it says that "If true only the size will be returned for the file path instead of the contents". I don't understand what does the size for the file path mean.
Besides, when I followed the example python script below to request the size, the result was 158 instead of 9.6kb ( the actual size). What does 158 mean?
Thanks in advance.
# This code sample uses the 'requests' library:
# http://docs.python-requests.org
import requests
import json url = "http://{baseurl}/rest/api/latest/projects/{projectKey}/repos/{repositorySlug}/browse/{path}"
headers = { "Accept": "application/json" }
response = requests.request( "GET", url, headers=headers )
print(json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": ")))
Hey @Dongying_Cao ,
158 is the number of lines in the file. append ?size=true to your request, and you'll get a response like {'size': 9600}
r = requests.get(f'{baseurl}/rest/api/latest/projects/{projectKey}/repos/{repositorySlug}/browse/{path}?size=true')
print(r.json())
The source of confusion here is that "size" has two different meanings.
When no other options are set, the standard /{path} resource returns the contents of the file. Each line is returned as a separate item inside a lines array. As with all our API's, the returned JSON has a number of standard values as well: limit (max number of results to be returned in a request), isLastPage (if results exceed the limit and are spread over multiple pages, this tells you whether you've reached the last page), and size, which in this context just means, the number of results being returned in this request, which is the number of lines in the file, or in the lines array.
You asked:
However, it says that "If true only the size will be returned for the file path instead of the contents". I don't understand what does the size for the file path mean.
When you do set size=true, that's when you're actually querying the file size. You won't get the file contents back, you'll get the file size value instead.
Even though the word "size" is used both times, it's being used to mean different things, and our API documentation is only referring to the explicit use of this option to get file size instead of the file contents.
I hope that clears it up!
Cheers,
Dave
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.