0

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?"

  • Does this answer your question? [Uploading file to HTTP via Powershell](https://stackoverflow.com/questions/38164723/uploading-file-to-http-via-powershell) – shahkalpesh Apr 12 '23 at 06: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: https://www.geeksforgeeks.org/http-headers-content-type/?force_isolation=true – jdweng Apr 12 '23 at 09: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. – Jerkle Berry Apr 12 '23 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"](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-restmethod?view=powershell-7.3#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. – Jerkle Berry Apr 12 '23 at 12:41
  • Multi-part is MIME where each attachment starts on a new line with two dashes. See : https://learn.microsoft.com/en-us/previous-versions/office/developer/exchange-server-2010/aa563375(v=exchg.140)?force_isolation=true – jdweng Apr 12 '23 at 13:00
  • @jdweng I understand the MIME format, but Invoke-RestMethod's -Form parameter should simplify all this, I thought. Are you saying I can't use -Form? – Jerkle Berry Apr 12 '23 at 17:10
  • Read definition of form. Form expects a Dictionary input. See : https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-restmethod?view=powershell-7.3 – jdweng Apr 12 '23 at 17:21
  • @jdweng I _am_ formatting as dictionary input. In my example, I define `file` and `id` as keys. – Jerkle Berry Apr 12 '23 at 21:44
  • You asked the question : "Does the byte array need to be converted to another format to use the form parameter in Invoke-WebRequest?" The answer is Yes. It need to be a dictionary. – jdweng Apr 13 '23 at 08:59
  • Shouldn't the byte array go into the `-Body` of your web request? – Santiago Squarzon Apr 22 '23 at 17:29
  • Hi @Santigo Squarzon, I'm trying to get this working with the `-Form` parameter because there are multiple form fields. Microsoft's [docs](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-webrequest?view=powershell-7.3#-form) says `-Form` and `-Body` are mutually exclusive. – Jerkle Berry Apr 24 '23 at 12:41

0 Answers0