I would like to convert a command to generate a masterkey from curl to PowerShell but I never did things like that before..
The objective is to access to an ovea Api but first I have to generate a masterkey.
Here is the curl command (tested on centos and works):
curl -L -H 'Content-Type: application/json' -H 'Authorization: Basic Yw...=' -X POST https://dnsadmin.test.com/apikeys --data '{"description": "masterkey","domains":["testapi.com"], "role": "User"}'
And here is what I tried:
$headers = #{}
$headers.Add = ("Authorization",'Basic Yw...=')
$headers.Add {-description = "masterkey","domains" ["testapi.com"], "role": "User"}
Invoke-RestMethod -Method Post -Uri https://dnsadmin.test.com/apikeys -Headers $headers -ContentType "application/json"
If any of you have an idea I would take it thank you.
When you specify --data with curl it is sent as the body of the request. In the Powershell example you're trying to do some stuff with the header that doesn't make sense.
This would be the equivalent to your curl command:
$headers = #{
Authorization = 'Basic Yw...='
}
Invoke-RestMethod -Method Post -Uri https://dnsadmin.test.com/apikeys -Headers $headers -ContentType "application/json" -Body '{"description": "masterkey","domains":["testapi.com"], "role": "User"}'
Related
This curl command works:
curl -v -X POST https://subdomain.playvox.com/api/v1/files/upload?context=quality -H "Content-Type: multipart/form-data" -u [username]:[password] -F file=#c:\path\to\file.wav
But I am unable to perform the same thing in PowerShell using the Invoke-RestMethod cmdlet. Here's my attempt:
$file_contents = [System.IO.File]::ReadAllBytes($($file_path))
Invoke-RestMethod -Uri "https://subdomains.playvox.com/api/v1/files/upload?context=quality" -Method Post -ContentType "multipart/form-data" -Headers #{ "Authorization" = "Basic $($playvox_base64_auth)" } -Body #{ file = $file_contents }
When run the API responds with invalid_param, "file" is required. However I confirmed the call to ReadAllBytes succeeds and gives back the raw file data. It seems like PowerShell is not sending the request body in the right format? I've looked at several other answers here and documentation online and nothing I found has worked.
Discovered there is a -Form parameter in Powershell 6 and later. I updated my powershell and used the following instead:
$file_contents = Get-Item $file_path
Invoke-RestMethod -Uri "https://subdomains.playvox.com/api/v1/files/upload?context=quality" -Method Post -ContentType "multipart/form-data" -Headers #{ "Authorization" = "Basic $($playvox_base64_auth)" } -Form #{ file = $file_contents }
And it worked.
Am Currently trying to query the authToken
$uri = 'http://xxx.centreon/api/index.php?action=authenticate'
$headers = #{
'username' = 'exemple'
'password' = 'exemple'
}
Invoke-RestMethod -Uri $uri -Method Post -Headers $headers -ContentType application/x-www-form-urlencoded
But am having bad parameters
Via linux : curl -s -d "username=exemple&password=exemple" -H "Content-Type: application/x-www-form-urlencoded" -X POST http://xxx.centreon/api/index.php?action=authenticate
{"authToken":"NWU5OWM0YjRhMjIzMDYuMzg2NzYyMDY="}-
So this is a vague question but i do see how things might be wrong.
First the Username and Password are usually in the Body not the Header.
In your curl command I see its being passed as -d which would be the Body
$uri = 'http://xxx.centreon/api/index.php?action=authenticate'
$Body = #{
'username' = 'exemple'
'password' = 'exemple'
}
Invoke-RestMethod -Uri $uri -Method Post -body $Body -ContentType "application/x-www-form-urlencoded"
I have follwoing curl command which I would like to convert to PowerShell (PowerShell v3.0):
curl -s -k -H 'Authorization: api_key 1234567890' -F upload_file=#"C:\path\to\file" -X POST "https://my.url.com/submit"
What I have so far:
$Body= #{ "upload_file" = "C:\path\to\file" };
$Headers = #{ "Authorization" = "api_key 1234567890" };
$Uri = "https://my.url.com/submit"
Invoke-WebRequest -Method Post -Uri $Uri -H $Headers -Body $Body
I think the form parameter is the issue.
Thanks in advance.
I need to convert the following command from bash curl to powershell syntax:
curl -s --head --header "PRIVATE-TOKEN:XXXXXX" "https://gitlab.XXXXXX/api/v4/projects/${id}/issues?state=all&per_pages=100"
This one in particular to get value of X-Total-Page from results.
I've tried to convert with this, but doesn't works:
function getPageNumbers ($myId)
{
$privateToken = "myToken"
$headers = #{"PRIVATE-TOKEN"="$privateToken"}
$url = "https://gitlab.XXXXXX/api/v4/projects/$myId/issues?state=all&per_pages=100"
$result = Invoke-RestMethod -Method Head -Uri "$url" -Header $headers -ContentType "text/json"
return $result
}
maybe because the "Head" method is used only for Invoke-Webrequest ?
Any suggestions?
Thank you in advance!
I am not seeing the body. But you can add that. You can do something like this:
$privateToken = "myToken"
$headers = #{"PRIVATE-TOKEN"="$privateToken"}
Invoke-WebRequest -Uri "https://gitlab.XXXXXX/api/v4/projects/$myId/issues?state=all&per_pages=100" -ContentType "application/json" -Headers $headers -Method Post
I would also suggest you to go through the CURL to Invoke-WebRequest
Hope it helps.
I'm trying to use the Invoke-RestMethod cmdlet in PowerShell 3 and 4, to upload a large binary file using a REST API's multipart/form-data upload. Here is a working cURL example on how to perform what I want to do in PowerShell:
curl -i -k -H "accept: application/json" -H "content-type: multipart/form-data" -H "accept-language: en-us" -H "auth: tokenid" -F file="#Z:\large_binary_file.bin" -X POST "https://server/rest/uri2"
I would love to see a working example on how to use Invoke-RestMethod to POST a multipart/form-data. I found a blog post from the PowerShell team showing how to use Invoke-RestMethod to upload to OneDrive (aka SkyDrive), but doesn't work well. I'd also like to avoid using System.Net.WebClient if at all possible. I also found another thread here on Stackoverflow, but it really didn't help much.
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }
$server = "https://server"
uri = "/rest/uri1"
$headers = #{"accept" = "application/json"; "content-type" = "application/json";"accept-language" = "en-us"}
$body = #{"userName" = "administrator"; "password" = "password"}
$method = "POST"
#Get Session ID
$resp = Invoke-RestMethod -Method $method -Headers $headers -Uri ($server+$uri) -body (convertto-json $Body -depth 99)
$sessionID = $resp.sessionID
#Upload file
$uri = "/rest/uri2"
$headers = #{"accept" = "application/json";"content-type" = "multipart/form-data"; "accept- language" = "en-us"; "auth" = $sessionID}
$medthod = "POST"
$largeFile = "Z:\large_binary_file.bin"
I have tried both ways of using Invoke-RestMethod:
Invoke-RestMethod -Method $method -Headers $headers -Uri ($server+$uri) -InFile $largeFile
or
$body = "file=$(get-content $updateFile -Enc Byte -raw)"
Invoke-RestMethod -Method $method -Headers $headers -Uri ($server+$uri) -body $body
I notice couple of mistakes in your invoke statement. First, you need to use POST keyword instead of string value. Second, make sure that Content-Type is set to multipart/form-data. So this is my revised statement -
$uri = "http://blahblah.com"
$imagePath = "c:/justarandompic.jpg"
$upload= Invoke-RestMethod -Uri $uri -Method Post -InFile $imagePath -ContentType 'multipart/form-data'
You can check the response from the server by checking $upload.