3

I'm trying to convert this working request done in Cygwin to Powershell:

Cygwin (Working):

curl -k -u curl:Password -X PUT -F "file=$($_)" https://$($appliance)/wse/customblockpage/file/en

Powershell (not working):

Invoke-Webrequest -Uri "https://$($appliance)/wse/customblockpage/file/en" -Method Put -Infile "$homePath\$($_)" -Credential $cred

Here is the error I get:

Invoke-Webrequest : { "Error": "No file part in file", "Result": "Failure" } At line:1 char:1 + Invoke-Webrequest https://{IP Address Masked}/wse/customblockpage/file/en ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

zzxyz
  • 2,953
  • 1
  • 16
  • 31

1 Answers1

0

The { "Error": "No file part in file", "Result": "Failure" } is actually the error response from the web server and not a specific PowerShell error message.

In your cURL invocation, you are specifying form data with the -F flag, yet you don't appear to be doing the same in PowerShell.

In PowerShell, you can specify form data using the -Body flag like this:

Invoke-Webrequest -Uri "https://example.com/" -Method Put -Body @{ "file" = "hello.txt" }

If you need to send the actual content of the file, then you can use this as your -Body argument:

-Body @{ "file" = (Get-Content hello.txt) }
Don Cruickshank
  • 5,641
  • 6
  • 48
  • 48
  • Thank you. That is a good starting point. I went ahead and modified to: Invoke-Webrequest -Uri "https://$($appliance)/wse/customblockpage/file/en" -Method Put -Body @{ "file" = "@./$($_)" } -Credential $cred -ContentType "multipart/form-data" This is the new error: "Language": "en", "Message": "Failure", "Result": "Failure" Any ideas? I tried changing from Put to Post, but I get the same effect. – Basam Yacoub Oct 17 '18 at 03:43
  • If it must be sent as `multipart/form-data` then that is already answered here: https://stackoverflow.com/questions/22491129/how-to-send-multipart-form-data-with-powershell-invoke-restmethod – Don Cruickshank Oct 17 '18 at 09:16
  • In my comment it is set as contentType "mulitpart/form-data" – Basam Yacoub Oct 18 '18 at 02:57
  • "https://$($appliance)/wse/customblockpage/file/en" -Method Put -Body @{ "file" = "@./$($_)" } -Credential $cred -ContentType "multipart/form-data" – Basam Yacoub Oct 18 '18 at 02:57
  • Just setting the content type isn't enough. Windows PowerShell doesn't support `multipart/form-data` yet and so you will need to use the link I posted if it turns out to be necessary. – Don Cruickshank Oct 18 '18 at 12:32
  • Also, `"@./$($_)"` doesn't make sense in PowerShell. The `@` is curl syntax for importing the content from a file. I've edited my answer to show the PowerShell equivalent. – Don Cruickshank Oct 18 '18 at 12:33