0

I'm trying to find the equivalent of this curl command in PowerShell with Invoke-webrequest :

curl -k https://url/api \
-H "Authorization: mykey" \
-F "json=@test.json;type=application/json" \
-F "file=@test.txt; type=application/octet-stream"```

-H is ok, but I didn't find for two -F option.

Any idea ?

Many thanks.
Greg
  • 1

2 Answers2

0

It seems like you should be able to do -Form ... -Form ... but you can't, you need to build a multipart form object to submit.

see example 5 of the Microsoft docs https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-webrequest?view=powershell-7.1

or this https://stackoverflow.com/a/65093133/89584 answer

or just install curl on windows (https://stackoverflow.com/a/16216825/89584) and use curl's nicer API

Malcolm
  • 1,168
  • 12
  • 23
  • If my answer doesn't work, example 5 as Malcom mentions above should get you heading in the right direction. – FoxDeploy Mar 29 '21 at 15:21
0

The -f switch in cUrl is used for a multi-part formdata content type. PowerShell fortunately natively supports it, here's a generic example to get you started.

$Uri = 'https://api.contoso.com/v2/profile'
$Form = @{
    firstName  = 'John'
    lastName   = 'Doe'
    email      = 'john.doe@contoso.com'
    avatar     = Get-Item -Path 'c:\Pictures\jdoe.png'
    birthday   = '1980-10-15'
    hobbies    = 'Hiking','Fishing','Jogging'
}
$Result = Invoke-WebRequest -Uri $Uri -Method Post -Form $Form

And for your specific scenario, something like this should get you moving in the right direction.

$url = 'https://url/api'
$Form = @{
    json       =  Get-Content .\test.json
    file       =  Get-Content .\test.txt
}

$Result = Invoke-WebRequest -Uri $Uri -Method Post -Form $Form -Token "mkey"

FoxDeploy
  • 9,619
  • 2
  • 26
  • 41