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

1 Answer 1

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) }
Sign up to request clarification or add additional context in comments.

6 Comments

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.
If it must be sent as multipart/form-data then that is already answered here: stackoverflow.com/questions/22491129/…
In my comment it is set as contentType "mulitpart/form-data"
"https://$($appliance)/wse/customblockpage/file/en" -Method Put -Body @{ "file" = "@./$($_)" } -Credential $cred -ContentType "multipart/form-data"
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.