0

I am trying to upload a jpg to a web server using a POST method.

Here are the fields I'm submitting to:

 <input type="file" name="myFile">
 <input type="submit" value="Upload">

This is the powershell error I get when I run the ps1: + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException

$FileContent = [System.IO.File]::ReadAllBytes("C:\<path to file>\4242.jpg")  
$Fields = @{"filename"=$FileContent ; "name"="myFile"}
Invoke-RestMethod -Uri $Uri -ContentType 'multipart/form-data' -Method Post -Body $Fields

The pcap shows the multipart dissector could not find a required parameter.

# Not reading in file correctly "name=myFile&filename=System.Byte%5b%5d" in pcap

Any idea what I'm missing?

Pand0ra
  • 3
  • 3
  • Use `Invoke-WebRequest` instead of `Invoke-RestMethod` – Mathias R. Jessen Feb 07 '17 at 20:17
  • Changing to Invoke-WebRequest still didn't upload the file and the pcap still shows the multipart dissector could not find a required parameter – Pand0ra Feb 07 '17 at 20:22
  • Now discovered that PowerShell does not (yet) have built-in support for making 'multipart' (i.e. binary file upload compatible) form uploads. Anyone have another idea how to achieve this? – Pand0ra Feb 07 '17 at 21:34

1 Answers1

0

Give this a shot. It is a PowerShell port of this answer.

Add-Type -AssemblyName System.Net.Http

$imageBytes = [System.IO.File]::ReadAllBytes("C:\<path to file>\4242.jpg")  
$HttpClient = New-Object -TypeName System.Net.Http.HttpClient
$form = New-Object -TypeName System.Net.Http.MultipartFormDataContent      
$byteArrayContent = New-Object System.Net.Http.ByteArrayContent -ArgumentList @(,$imageBytes)
$form.Add($byteArrayContent , "myFile", "4242.jpg")
$response = $httpClient.PostAsync($uri, $form).Result
$response.EnsureSuccessStatusCode()
$httpClient.Dispose()
Community
  • 1
  • 1
Adam Driscoll
  • 9,030
  • 7
  • 57
  • 98