1

My Powershell 7 script reads JPG files from a content service.

$fetch = @{
   Uri = 'https://content.site/media?id=82348'
   Method = GET
}
$fetchResponse = Invoke-WebRequest @fetch 

Image data loads into $fetchResponse.content as a byte array. Now I must send to a local web server.

$ingest = @{
   Uri = 'https:\\internal.site\ingest'
   Method = POST
   Form = @{
      file = $fetchResponse.content
      id = '82348'
   }
}
$ingestResponse = Invoke-WebRequest @ingest

Sending the byte array directly as above does not work. Image data is not detected in the received form.

If I first save the image locally and then use Get-Item to point to the file, it is successful. I do not want to save the images locally if avoidable.

Does the byte array need to be converted to another format to use the form parameter in Invoke-WebRequest?

Edit: after a bit of circular questioning and answering in the comments, maybe the question is better asked as, "How do I convert a variable's byte array to System.IO.FileInfo without having to write a file to disk?"

11
  • Does this answer your question? Uploading file to HTTP via Powershell Commented Apr 12, 2023 at 6:41
  • You need to take the body of the $fetchResponse and put into the body of the $ingest. Your POST is referencing a filename for the image instead of the body. When you send the POST you also must include the HTTP Header Content-Type and indicate the data in binary data. See: geeksforgeeks.org/http-headers-content-type/… Commented Apr 12, 2023 at 9:10
  • @shahkalpesh No, the first answer uses -Infile, which is a local file reference. I already have the image data in my response variable, so I don't want to have to write the file locally first. Plus, that answer doesn't allow the other form fields to be sent. The second answer has the same limitations, a locally saved file and no way to include the other form fields. Commented Apr 12, 2023 at 12:15
  • @jdweng I don't understand. The file is one field of many in the form. If I send as the entire body, how are other form fields included? The Form parameter looks like it should work, but in "Example 4: Simplified Multipart/Form-Data Submission" on Microsoft's Invoke-RestMethod page, it's sending info of a local file as System.IO.FileInfo. I want to reference the byte array already retrieved, not a local resource. Commented Apr 12, 2023 at 12:41
  • Multi-part is MIME where each attachment starts on a new line with two dashes. See : learn.microsoft.com/en-us/previous-versions/office/developer/… Commented Apr 12, 2023 at 13:00

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.