Fetching the source via url “…/api/sources/key=PROJ:src/test_file&branch=b1” results in fetching from the default branch instead (usually master), which results in an error if the file does not exist on that branch.
Note that the URL “…/api/issues” works correctly with the branch parameter.
Yes, you’re correct. You cannot select branch from this API. Actually this API is not actively evolved since it is no longer used by SonarQube and there are alternatives API that do the job fine:
If you only want the raw source file you should use api/sources/raw
If you want the SCM data you should use the api/sources/lines. This API is internal though.
Both the above API support the branch parameter.
Now I am not quite sure why you want to fetch source code from SonarQube. This is 2nd hand information. You should get it from the SCM itself.
It is the SCM data I needed for a report which I generate using the Python sonarqube library.
Originally the report was generated on another server without access to the GIT repository (post build/scan). That is no longer the case so I can fetch directly as you mention.