Jump to content

Recommended Posts

Posted

Hoping I could get an example of a properly formatted PowerShell script that would upload a file via the API, I have been doing API testing via the Hornbill demo site and so far it's being going great. The only hitch I have had is demoing a file upload via the API, I was attempting to upload a tiny PDF file (48KB) but ended up with a 404. I could be doing it right but maybe the demo site doesn't have /dav/session available? 

Any assistance in making sure I'm doing it right would be great, I keep pouring over the information in the wiki, documentation library and this forum but feel like I have missed something that would make it work. Note: I have not uploaded to a WebDav location before so the process is new in general to me.

$Endpoint = "https://mdh-p01-api.hornbill.com/demo/dav/session/Ad.pdf"
$File = "C:\Temp\Ad.pdf"
$Header = @{
	"Authorization" = "ESP-APIKEY XXXXXXXXXXXXXXXXXXXXXXXX"
}
$Parameters = @{
	Method      = "POST"
	Uri         = $Endpoint
	Headers     = $Header
	Body        = $File
	ContentType = "application/pdf"
}

 

Posted

Hi @William Weiszbrod,

Easy fix - your HTTP verb should be PUT instead of POST :) 

Note - that demo instance is accessible to the public, and is not intended for use as a sandbox for live customers. Uploading files in there that have PII, or any other type of private data, would obviously be a very bad thing! 

Cheers,

Steve

Posted

Excellent, I didn't get a 404. Should I get a return of nothing if it works? I was hoping it would return some kind of confirmation.

the PDF is just me smashing my keyboard against my head, so it's just a bunch of gibberish. 😄

Posted

@William Weiszbrod There's no response payload if it's successful - but you should get a status code of 200, 201 or 204 depending on the request. From memory, I think PUTting to that endpoint will return a 200. 

Cheers!

Steve

 

 

Posted

Thanks again! I was confused as apparently Invoke-RestMethod doesn't  return the status code, it only returns messages placed in the body of the return message. Snagging the status code from Invoke-RestMethod you have to specify a variable in the parameters, then you can query that variable to know the status of the response. 

Just in case anyone else runs across this here is a fully functional PowerShell Script for uploading via the API with the response parameter set.

# UPLOAD ATTACHMENTS
$Endpoint = "(your instance here)/dav/session/(name of file with extension)"
$File = "[(local file full path name)]"
$Header = @{
	"Authorization" = "ESP-APIKEY XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
}
$Parameters = @{
	Method      = "PUT"
	Uri         = $Endpoint
	Headers     = $Header
	Body        = $File
	ContentType = "application/pdf" #change this to match the file type
	StatusCodeVariable = "StatusCode" #This is the variable for the status code
}
$Capture = Invoke-RestMethod @Parameters
$StatusCode #if succesful this will return 200

 

Posted

Last update as apparently uploading with the above code "works" as in an object with the name exists the PDF was in fact corrupted after upload. I got some wild file sizes when uploading, tried a couple of different methods to get it to play nice but in the end I had to convert the whole file into a bitstream and then upload that stream to prevent the corruption. For the sake of completeness here is what I did for anyone who needs a method to upload files via he API without corruption using PowerShell, note this works for PDF but most likely works for any file type as the file is a bit stream.

 

# UPLOAD ATTACHMENTS
$Endpoint = "[your instance]/dav/session/Ad.pdf"
$fileBytes = [System.IO.File]::ReadAllBytes("[full path to local file]")
$file = New-Object System.IO.MemoryStream
$file.Write($fileBytes, 0, $fileBytes.Length)
$file.Seek(0, [System.IO.SeekOrigin]::Begin) | Out-Null

$Header = @{
	"Authorization" = "ESP-APIKEY [your api key]"
}
$Parameters = @{
	Method      = "PUT"
	Uri         = $Endpoint
	Headers     = $Header
	Body        = $File
	ContentType = "application/pdf"
	StatusCodeVariable = "StatusCode"
}
$CaptureUpload = Invoke-RestMethod @Parameters
$StatusCode # Will return 200 when succesful 

 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...