After pushing I want to start a release – also with the REST API.
I have been looking at the Swagger interface and can successfully do a number of GETs to receive information about the project I want to release.
I’ve also looked/searched in the examples at https://github.com/OctopusDeploy/OctopusDeploy-Api, but I haven’t found good starting points for the REST calls I want to make.
Thanks! One small follow-up: I don’t understand the push example well enough to replicate it in a different language. Quite a lot seems to happen in $packageFileStream – more than in a usual POST
Unfortunately, the /api/packages/raw endpoint is not documented in the Swagger I have.
Is there somewhere I can read more about how to push?
I’ve pasted the code and added some comments. I’m hoping that they help and not insult your intelligence
# Create web request object
$webRequest = [System.Net.HttpWebRequest]::Create($packageUrl);
# Configure web request object
$webRequest.AllowWriteStreamBuffering = $false
$webRequest.SendChunked = $true
$webRequest.Accept = "application/json";
$webRequest.ContentType = "application/json";
$webRequest.Method = "POST";
$webRequest.Headers["X-Octopus-ApiKey"] = $apiKey;
# Create FileStream object for package that we're uploading
$packageFileStream = new-object IO.FileStream $packageFilePath,'Open','Read','Read'
# Not sure why this is indented - define text string and append the ticks (https://docs.microsoft.com/en-us/dotnet/api/system.datetime.ticks?view=netframework-4.8)
$boundary = "----------------------------" + [System.DateTime]::Now.Ticks.ToString("x");
# Convert the $boundary string variable into a byte array of ASCII encoding
$boundarybytes = [System.Text.Encoding]::ASCII.GetBytes("`r`n--" + $boundary + "`r`n")
# Set the content type, defining what the boundary is
$webRequest.ContentType = "multipart/form-data; boundary=" + $boundary;
# Write the boundary byte array to the web request stream
$webRequest.GetRequestStream().Write($boundarybytes, 0, $boundarybytes.Length);
# Define header, include filename of package in header
$header = "Content-Disposition: form-data; filename="""+ [System.IO.Path]::GetFileName($packageFilePath) +"""`r`nContent-Type: application/octet-stream`r`n`r`n";
# Convert header string into byte array using ASCII encoding
$headerbytes = [System.Text.Encoding]::ASCII.GetBytes($header);
# Write header to stream
$webRequest.GetRequestStream().Write($headerbytes, 0, $headerbytes.Length);
# Copy contents of file stream object into the web request stream object
$packageFileStream.CopyTo($webRequest.GetRequestStream());
# Write boundary byte array to request stream
$webRequest.GetRequestStream().Write($boundarybytes, 0, $boundarybytes.Length);
# Flush and close the web request stream
$webRequest.GetRequestStream().Flush();
$webRequest.GetRequestStream().Close();
# Close filestream
$packageFileStream.Close();
$packageFileStream.Dispose();
# Get response from web server
$webResponse = $webRequest.GetResponse();
Write-Host $webResponse.StatusCode $webResponse.StatusDescription;
$webResponse.Dispose();
Thank you for the detailed explanations! I will give it a try
I also have the green “POST /api/packages/raw”, but when I click on it there is just a note saying that documentation will be added in a future version.