0

I have this cURL command that I must adapt to Powershell in order to upload different files. In order to do that, I have mandatory fields that I need use in the request. I've tried like a dozen of different scripts, nothing works.

Can someone help me out with this? It should be plain simple, but I am missing something.

cURL command:

curl -H "Authorization: Bearer xxx" 
-F "parentDirectoryId=1" 
-F "name=AutoUpload" 
-F "contents=@C:\temp\test.pdf" 
https://url/v1/api/files?
  • Possible duplicate of [How to use Invoke-RestMethod to upload jpg](https://stackoverflow.com/questions/42395638/how-to-use-invoke-restmethod-to-upload-jpg) – Dan Wilson Sep 25 '18 at 21:13
  • I've checked that link before posting, but my request is different @DanWilson, since it contains other parameters as well. – Dorel Pureca Sep 25 '18 at 21:20
  • Then you may need to construct a `WebRequest` object instead. https://stackoverflow.com/a/45409728/3608792 – Dan Wilson Sep 25 '18 at 21:26
  • I've reviewed that post as well and it didn't help. @DanWilson, perhaps you know how to write the entire script? – Dorel Pureca Sep 25 '18 at 21:30

1 Answers1

0

This is going to depend on which version of Powershell you are using. If you have Powershell 6 you can use the simple method below which uses the form parameter. If you use another version you can use the more complicated example #4 which is outlined on the Microsoft Docs:

$Uri = 'https://url/v1/api/files?'
$Form = @{
    parentDirectoryId= '1'
    name = 'AutoUpload'
    contents= Get-Item -Path 'C:\temp\test.pdf'
}
$token = ConvertTo-SecureString "xxx" -AsPlainText -Force
$Result = Invoke-RestMethod -Uri $Uri -Method Post -Form $Form -Authentication Bearer -Token $token

This example uses the new Authentication and Form parameters in Invoke-RestMethod. Depending on your Authentication type, you will need either a Token or Credentials parameter with additional information. The Form parameter simplifies what was previously a complicated process for adjusting the body or URI per request.

Jordan
  • 393
  • 1
  • 5
  • Hey! Thanks for the reply @Jordan. I've tested your script both on Powershell 5 and 6 and it doesn't work. Here's the response error: Invoke-RestMethod : Cannot bind parameter 'Headers'. Cannot convert the "-Authorization" value of type "System.String" to type "System.Collections.IDictionary". At line:1 char:64 – Dorel Pureca Sep 26 '18 at 07:32
  • I have adapted the script found on Microsoft Docs Example 4 and that doesn't do it either. – Dorel Pureca Sep 26 '18 at 07:42
  • Remove the headers parameter and it should work for pwsh 6, it's a copy paste error. I'll update my code in a bit. – Jordan Sep 26 '18 at 13:44
  • Jordan, here's the error I get: Invoke-RestMethod : Cannot bind parameter 'Token'. Cannot convert the "xxx" value of type "System.String" to type "System.Security.SecureString". At line:1 char:93 + ... Uri $Uri -Method Post -Form $Form -Authorization Bearer -Token $token + ~~~~~~ + CategoryInfo : InvalidArgument: (:) [Invoke-RestMethod], ParameterBindingException + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.InvokeRestMethodCommand – Dorel Pureca Sep 26 '18 at 14:53
  • Secure strings are a bit complicated. The simple method is to update `$token = "xxx"` to be `$token = (Read-Host -AsSecureString)`. You will have to type the bearer token into the host. If you don't have a problem leaving the token in plain text, you can use `$token = ConvertTo-SecureString "xxx" -AsPlainText -Force` Otherwise see [ConvertTo-SecureString](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.security/convertto-securestring?view=powershell-6). – Jordan Sep 26 '18 at 15:25
  • Thanks again. Invoke-RestMethod : A parameter cannot be found that matches parameter name 'Authorization'. – Dorel Pureca Sep 26 '18 at 15:47
  • Additionally change `-Authorization` to `-Authentication`, more copy paste issues. I've updated the answer to reflect this change. – Jordan Sep 26 '18 at 15:47
  • This is just like pingpong.. perhaps we should try another communication channel and post the solution? this is what I get now: Invoke-RestMethod : {"type":"http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html","title":"Not Acceptable","status":406,"detail":"Unable to resolve Accept header to a representation"} – Dorel Pureca Sep 26 '18 at 15:51