I want to download from a private repository in GitHub, so I need to pass the headers Authorization: token ${GITHUB_TOKEN}
and Accept: application/vnd.github.v3.raw
.
For example, with curl: curl -H "Authorization: token ${GITHUB_TOKEN}" -H 'Accept: application/vnd.github.v3.raw' -o file.yaml -L https://api.github.com/repos/<owner>/<repo>/contents/file.yaml
Looking for information about how to download a file in Kotlin, I found a few examples, and I’m using the following:
fun downloadFile(url: URL, fileName: String) { url.openStream().use { Files.copy(it, Paths.get(fileName)) } }
How can I pass those two headers?
Advertisement
Answer
Found a link that helped me with this:
fun downloadFile(url: URL, fileName: String, githubToken: String) { url.openConnection().apply { setRequestProperty("Accept", "application/vnd.github.v3.raw") setRequestProperty("Authorization", "token "+ githubToken) }.getInputStream().use { Files.copy(it, Paths.get(fileName)) } }