Powershell likes to auto convert your API’s JSON response to a PSCustomObject. This normally would be what I want. Since there is no name key PS doesn’t do a good job of converting the response to a PSObject. Here is a example of my attempts at getting clean data. My last attempt I used the HTTP client assembly thinking this would skip the data type conversions but it doesn’t and makes me think it’s the HTTP assembly making the conversion.
Add the necessary .NET assembly
Add-Type -AssemblyName System.Net.Http
Create the HttpClient object
$client = New-Object -TypeName System.Net.Http.Httpclient
Get the web content.
$task = $client.GetByteArrayAsync(“https://circleci.com/api/v1.1/project/$vcs_type/$username/$project/$build_number/artifacts?circle-token=$CIRCLE_TOKEN”)
Wait for the async call to finish
$task.wait();
Write to file
results:
(({
:path “src.orig/bla.bla”,
:pretty-path “src/bla.bla”,
:node-index 0,
:url “https://15-198716507-gh.circle-artifacts.com/0/src/bla.bla”
} {
:path “src/Benchmark.mxml”,
:pretty-path “src/Benchmark.mxml”,
:node-index 0,
:url “https://15-198716507-gh.circle-artifacts.com/0/src/Benchmark.mxml”
}…continued
As you can see this is not JSON or YAML. Let’s try the built-in tools like invoke-restmethod and invoke-webrequest.
Invoke-RESTMethod -uri https://circleci.com/api/v1.1/project/$vcs_type/$username/$project/$build_number/artifacts?circle-token=$CIRCLE_TOKEN -Method GET
output:
({
:path “src.orig/bla.bla”,
:pretty-path “src/bla.bla”,
:node-index 0,
:url “https://15-198716507-gh.circle-artifacts.com/0/src/bla.bla”
} {
:path “src/Benchmark.mxml”,
:pretty-path “src/Benchmark.mxml”,
:node-index 0,
:url “https://15-198716507-gh.circle-artifacts.com/0/src/Benchmark.mxml”
}…continued
Invoke-WebRequest -Uri https://circleci.com/api/v1.1/project/$vcs_type/$username/$project/$build_number/artifacts?circle-token=$CIRCLE_TOKEN -Method GET -ContentType ‘application/json’ -UseBasicParsing
StatusCode : 200
StatusDescription : OK
Content : {40, 123, 58, 112…}
RawContent : HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Strict-Transport-Security: max-age=15724800
Vary: Origin
X-CircleCI-Identity: circle-www-api-v1-48735c64d-65pbv
X-Circleci-Scopes: :write-settings,:…
Headers : {[Access-Control-Allow-Origin, *], [Strict-Transport-Security, max-age=15724800], [Vary, Origin], [X-CircleCI-Identity, circle-www-api-v1-48735c64d-65pbv]…}
RawContentLength : 165064
I am getting the integers in the content of the web request. not what was expected. I know from the invoke-restmethod documentation that PS sees JSON and auto converts it to a PS object. but I don’t want this. I want to get my data and parse it to download all the artifacts. I have seen where if there is a name key then it may work correctly but it’s not as of now. I’m going to continue to search on how to get the raw JSON or another way to parse the data so I can download my artifacts from CircleCI. I’m trying to avoid using a non-PS tool or language to accomplish this task. I know I could use python or anther language for this task. but it seems like overkill for this. Does anyone know a native PS script way to accomplish this?