PowerShell Invoke-RestMethod to query Token - powershell

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"

Related

Convert a curl command to powershell

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"}'

Powershell how to get the result code from REST

I'm sending a POST request with ID/password and I need to get back a respond token, how can I get it and save it for later use in the script?
$loginUrl = "https://some-ip"
$params = #{
"username"="$username"
"password"="$password"
}
Invoke-WebRequest -Uri $loginUrl -Method POST -Body ($params|ConvertTo-Json) -ContentType "application/json"
Following your input:
$url = "https://some-ip"
$params = #{
"username" = $username
"password" = $password
} | ConvertTo-Json
$apiReturn = Invoke-RestMethod -Uri $url -Method POST -Body $params -ContentType "application/json"
$apiReturn can then be used as response.
Furthermore, you can use the SessionVariable parameter of Invoke-RestMethod.
$apiReturn = Invoke-RestMethod -Uri $url -Method POST -Body $params -ContentType "application/json" -SessionVariable sessionToken
$sessionToken.Headers.Add('Authorization', $apiReturn)
$sessionToken.Headers.Add('Content-Type', 'application/json')
In this scenario, you add the response token to 'Authorization' and forward the whole token to your subsequent API calls. Like this you only need to add $sessionToken and Content-Type for example is already provided.
Invoke-RestMethod -Method Post -Uri $url -WebSession $sessionToken
You can add more parameters to your Header in case it is required.

How to convert curl POST with headers authorization and form to PowerShell v3.0

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.

how to convert curl command in Invoke-RestMethod of powershell

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.

Using PowerShell Invoke-RestMethod to POST large binary multipart/form-data

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.