convert simple curl data request to powershell invoke-webrequest - json

Converting a very simple Plaid API curl data request to a powershell invoke-webrequest
what works:
curl -X POST https://tartan.plaid.com/balance \
-d client_id=client-id-value \
-d secret=secret-value \
-d access_token=access-token-value
What I'm trying unsuccessfully in Powershell
#test powershell api call
$hash = #{ client_id = "client-id-value";
secret = "secret-value"
access_token = "access-token-value"
}
$JSON = $hash | convertto-json
#Invoke-WebRequest -uri "https://tartan.plaid.com/balance" -Method POST -Body $JSON
This returns a plaid error 1100 (client id missing), so I know some API functionality is working, but it's not parsing input data correctly.
My biggest misunderstanding is how to translate a "curl -data" value into the proper Powershell parameter.
Is this a header value, body value? etc.

Unless the target url expects the POST body to be in JSON format, skip the ConvertTo-JSON step completely.
When the chosen HTTP method is POST, Invoke-WebRequest will automatically take all the keys in the hashtable supplied to the -Body parameter and construct a body payload similar to that of curl -d:
$POSTParams = #{
client_id = "client-id-value"
secret = "secret-value"
access_token = "access-token-value"
}
Invoke-WebRequest -Uri "https://tartan.plaid.com/balance" -Method POST -Body $POSTParams

Related

How to put a hyperlink in a MS Teams message sent from a Powershell script in a TeamCity build project?

I'm using TeamCity to build my project.
In one of my build steps, I put a Powershell script, which uses Webhook to send a message to a MS Teams channel.
$url = "https://..."
$body = #{
title = "MtTitle";
text = "Visit: $url";
} | ConvertTo-Json
$postBody = [Text.Encoding]::UTF8.GetByres($body)
Invoke-WebRequest -Method Post -Uri "https://mycorp.webhook.office.com/..." -Body $postBody -ContentType "application/json" -UseBasicParsing
As a result of the script above, a message is sent to the Teams channel as expected, but the URL (the string after Visit:) is shown as a plain text.
How is it possible to make it a clickable hyperlink?
Should I use a MessageCard as shown in the link below?
Get Build Job URL in TeamCity Build Step
You can create a link in teams like that:
[HereText](hereLink)
Code would look somthing like that:
$url = "https://..."
$urlText = "some"
$body = #{
title = "MtTitle";
text = "Visit [$urlText]($url)";
} | ConvertTo-Json
Invoke-WebRequest -Method Post -Uri "https://mycorp.webhook.office.com/..." -Body $postBody -ContentType "application/json" -UseBasicParsing

SurveyMonkey creating a collector in Powershell returning "The body provided was not a proper JSON string

I'm using Powershell to automate a survey creation, adding a collector, and sending the survey invitation - a pretty common use case. I was able to create a survey but I'm running into an issue in creating the collector. I created a collector via Postman but I get the error "1001 - The body provided was not a property JSON string" when using Powershell. I've tried everything I can think of but continue to get the error no matter how I create the JSON. Here is the code I'm using:
$SurveyCollector = [PSCustomObject]#{ type = 'email' }
$BodyText = $SurveyCollector | Select-Object -Property type |ConvertTo-Json -Depth 100 -Compress
$Results = Invoke-RestMethod -Uri $uri -StatusCodeVariable "ExtIDscv" -SkipHttpErrorChe -Method
Post -ContentType "application/json" -Authentication Bearer -Token $SecureToken -body -$BodyText
I'm sure I'm missing something simple. I'd appreciate any help.
The answer was more simple than what tripleee suggested. I had introduced a -$BodyText into the call and didn't catch it. I removed it and it works just fine.
$Results = Invoke-RestMethod -Uri $uri -StatusCodeVariable "ExtIDscv" -SkipHttpErrorChe -Method
Post -ContentType "application/json" -Authentication Bearer -Token $SecureToken -body -$BodyText

How do I fix a "cannot bind parameter 'uri'. Cannot convert the...." when converting json in powershell?

I am doing a Get-Weather project in powershell, where I pull data down from weatherapi.com . I am able to successfully connect to the website using an API key but, when I try to convert it from json in the script it doesn't work. The error I get is:
"Cannot bind parameter 'Uri'. Cannot convert the..."
I have tried so many different ways to write this:
$response = Invoke-RestMethod -uri $url -Method Get -ResponseHeadersVariable r -StatusCodeVariable s
$weatherobject = ConvertFrom-Json $url
The request for the website is:
$url = Invoke-WebRequest "http://api.weatherapi.com/v1/forecast.json?key=$key&q=$location&days=$Days"
Any help would be very much apperciated, thank you!
The input of the ConvertFrom-Json cmdlet is a JSON object. Look at the document below for more information
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/convertfrom-json?view=powershell-7.1#description
$url = "http://api.weatherapi.com/v1/forecast.json?key=$key&q=$location&days=$Days"
$response = Invoke-RestMethod -uri $url -Method Get -ResponseHeadersVariable r -StatusCodeVariable s
$weatherobject = ConvertFrom-Json $response

Convert Curl to Invoke-RestMethod

Problem
I am having a hard time converting a curl call into a Powershell Invoke-RestMethod call as Powershell doesn't really thow the most informative error messages (if any).
Curl call (Ubuntu)
token = "djsakldsakldjaslda"
host = "https://lalala.azuredatabricks.net/"
curl -X POST -H "Authorization: Bearer $(token)" $(host)/api/2.0/clusters/create -d $(cat my_file.json)
Invoke-RestMethod call (Powershell)
$token= "djsakldsakldjaslda"
$host = "https://lalala.azuredatabricks.net/"
Invoke-RestMethod -Method Post -Uri $host/api/2.0/clusters/create -Headers #{"Authorization" = "Bearer " + $token} -Body $(get-content my_file.json -raw | ConvertFrom-Json)
I have various formats for the body, but no matter what I send, I just get some HTML back for a login page. On Ubuntu with Curl everything works perfectly.
NOTE:
The problem seemed to be that PowerShell cannot handle double-"/" as in "https://lalala.azuredatabricks.net//api/2.0/clusters/create".
The strange part is that Invoke-RestMethod gets to the login page, but fails from there.
Use -InFile to upload a file. Don't forget to set the content type.
Wrapped for legibility (escaping the EOL works as line continuation in PowerShell, it looks funny because StackOverflow syntax highlighting cannot handle it):
Invoke-RestMethod `
-Method Post `
-Uri "$host/api/2.0/clusters/create"
-Headers #{
Authorization = "Bearer $token"
} `
-Infile my_file.json
-ContentType "application/json"
The body is expected to be JSON format, when you take the file and add the | ConvertFrom-Json the content becomes a PowerShell object.
So, you can remove the | ConvertFrom-Json and it should work :)

How to pass options to Rundeck job webhook URL

I have defined a webhook in Rundeck to run a particular job. This job has 3 options defined: ${option.VMName}, ${option.CPU} and ${option.Memory}. The job itself is defined as a local powershell script and executes as: powershell ${scriptfile} ${option.VMName} ${option.CPU} ${option.Memory}. This is tested and works fine.
I would now like to invoke the webhook POST URL so that the job is remotely triggered (from a web dashboard, using PowerShell) with these options defined. I tried, unsuccessfully, adding the options to the end of my URL:
http://mywebhookuri#myjobname?opt.VMName=$VMName&opt.CPU=$CPU&opt.Memory=$Memory
http://mywebhookuri#myjobname?VMName=$VMName&CPU=$CPU&Memory=$Memory
The following PowerShell code is being used to invoke the webhook:
$WebHookURI = "http://mywebhookuri#myjobname"
$header = #{}
$header.add("Content-Type","text/plain")
$body = #{} | ConvertTo-Json
$result = Invoke-RestMethod -Method Post -Uri $WebHookURI -Body $body -Headers $header
The documentation for the webhook plug-in and run-job usage state that "The JSON that is received by the plugin can be used to supply options, node filter, and the Run As user", but doesn't show a clear example of either.
How do I successfully pass these options to the webhook URL?
Following the documentation, you need to define the option in this way, and later call passing a JSON data, I did an example but using cURL:
curl -H "Content-Type: application/json" -X POST -d '{"field1":"hello world"}' http://yourhost:4440/api/34/webhook/3moY0Ru1zxl5gM0tpVlecJ5BN1LPyhsx#New_Hook
That is for this Job Definition example:
<joblist>
<job>
<context>
<options preserveOrder='true'>
<option name='opt1' />
</options>
</context>
<defaultTab>nodes</defaultTab>
<description></description>
<executionEnabled>true</executionEnabled>
<id>e97efb53-99a6-4e5a-80b7-a1b055866f43</id>
<loglevel>INFO</loglevel>
<name>HelloWorld</name>
<nodeFilterEditable>false</nodeFilterEditable>
<scheduleEnabled>true</scheduleEnabled>
<sequence keepgoing='false' strategy='node-first'>
<command>
<exec>echo ${option.opt1}</exec>
</command>
</sequence>
<uuid>e97efb53-99a6-4e5a-80b7-a1b055866f43</uuid>
</job>
</joblist>
To add some detail to MegaDrive68k's accepted answer (as this is essentially two questions):
I added the following to the "Options" field in the Rundeck webhook definition:
-VMName ${data.field1} -CPU ${data.field2} -Memory ${data.field3}
And the PowerShell code was modified as follows:
$WebHookURI = 'http://mywebhookuri#myjobname'
$header = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$header.add("Content-Type", "application/json")
$body = "{`n `"field1`" : `"$VMName`",`n `"field2`" : `"$CPU`",`n `"field3`" : `"$Memory`"`n}"
$result = Invoke-RestMethod -Method 'POST' -Uri $WebHookURI -Body $body -Headers $header
With these changes I was able to successfully invoke the Rundeck webhook with options.
a bit more concise version for PowerShell:
$WebHookURI = 'http://mywebhookuri#myjobname'
$header = #{}
$header.add("Content-Type", "application/json")
$body = #{
field1 = "$VMName";
field2 = "$CPU";
field3 = "$Memory"
} | ConvertTo-Json
$result = Invoke-RestMethod -Method 'POST' -Uri $WebHookURI -Body $body -Headers $header