0

I have a file which I download using the REST API. Just to emphasize that I tried with both commands: Invoke-RestMethod and Invoke-WebRequest

$uri = "https://bitbucket.org.dev/projects/TEST/repos/fa/browse/Packages/ATS.txt"
$API_KEY="ZTU2MT"
Invoke-WebRequest -Headers @{Authorization=$("Basic {0}" -f $API_KEY)} -Uri $uri  -OutFile ATS.txt

If I access that URI in browser file or download it manually file can be viewd without any issue in clear way.

This is the content of the file (its begining)

#
# exported extension module.
# source          ""
# timestamp (utc) "2020-03-30 12:06:23.3"
# ***** DO NOT EDIT! *****

But download file looks completely different (like it is in HTML format)

<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta http-equiv="X-..." 

Also I have some zip file which needs to be downloaded as well, but with it also I am getting invalid file which cannot be extracted (opened)

vel
  • 1,000
  • 1
  • 13
  • 35

1 Answers1

1

As written in MSDN, Invoke-WebRequest returns an object of type BasicHtmlWebResponseObject.

You need to select the Content property to get the text you are looking for.

Invoke-WebRequest -Headers @{Authorization=$("Basic {0}" -f $API_KEY)} -Uri $uri | Select-Object -Expand Content | Out-File -FilePath c:\somefile.txt

Update:

Check this post to find more info about downloading files from a private repository in BitBucket. https://stackoverflow.com/a/50222671/13440610

JeremyRock
  • 396
  • 1
  • 8
  • thanks for you assistance - but I am still getting the exactly same HTML format in my downloaded file with your command.. Do you know what can be wrong? Thanks – vel May 24 '20 at 20:00
  • Can you post more of the html you are getting? Check also the response code. If there is an error, itmight be related to Authorization. – JeremyRock May 24 '20 at 20:03
  • Thanks. Source of AccountingATS.txt - FA-Select - BitBucket – vel May 24 '20 at 20:11
  • I can for example without any issue, so that is why I thought it is not related to authorization - get a list of all folders and files in repository using the REST API. But not sure why I cannot retrieve the file itself. – vel May 24 '20 at 20:12
  • updated the answer. I think this is related to Bitbucket. You have a full example in the link. – JeremyRock May 24 '20 at 20:21
  • I am already encoding my credentials and they are passed since I succeed to retrieve some other artifacts. But on linked answer they are mentioned something that I should have my account inside the URL??? Did you also understood like that? "https://bitbucket.org///get/.zip" But how I should put my account here? I really do not understand what should be the URL syntax? As stated: with my URLs I can assess manually to my files but I do not have my account within the URL. Thanks... – vel May 24 '20 at 20:40
  • Hi. I added ?raw suffix at the end ant then it worked. It worked for txt but not for zip file so I switched back to my Invoke method (Invoke-WebRequest -Headers @{Authorization=$("Basic {0}" -f $API_KEY)} -Uri $uri -OutFile ATS.txt?raw) and then it worked for both formats... – vel May 24 '20 at 21:52