0

I want to send a serialized .json file to a PowerShell Azure Function, prettify it, then return the file to Flow for further processing. I cannot figure out how to do this.

In Flow:

  • Trigger: Button push
  • Action1: Get file content from OneDrive
    • Output:
    {
      "$content-type": "application/octet-stream",
      "$content": "eyJUb3BQYXJlbnQiOns...<truncated for this post>"
    }
  • Action2: Send HTTP Request
    • URI: https://test.azurewebsites.net/api/prettifyJson?code=<api key>
    • Method: POST
    • Body: body('Get_file_content') (output from Action1)

In Azure Function:

  • Default PowerShell run.ps1:
using namespace System.Net

# Input bindings are passed in via param block.
param($Request, $TriggerMetadata)

# Write to the Azure Functions log stream.
Write-Host "PowerShell HTTP trigger function processed a request."

# Interact with body of the request.
$content = $Request.Query.baz
if (-not $name) {
    $name = $Request.Body.baz
}

if ($name) {
    $status = [HttpStatusCode]::OK
    $body = "Hello $name"
}
else {
    $status = [HttpStatusCode]::BadRequest
    $body = "Please pass a name on the query string or in the request body."
}

# Associate values to output bindings by calling 'Push-OutputBinding'.
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
    StatusCode = $status
    Body = $content
})

Issues:

  1. A call to the function fails with Please pass a name on the query string or in the request body. I can see why, but do not know what syntax to replace in run.ps1
  2. I have no idea what syntax to use to:

    a. Receive the .json file

    b. Convert it to pretty json

    c. Repackage the .json file

    d. Send it back to Flow

Looking for guidance.

ericOnline
  • 1,586
  • 1
  • 19
  • 54

1 Answers1

1

About number one, seems you're missing the name in the url.

try to replace from this:

https://test.azurewebsites.net/api/prettifyJson?code=

to this:

https://test.azurewebsites.net/api/prettifyJson?name=test&code=

about your second question, the you'll need to parse the body content as Hashtable. try to combine:

$hash = $Request.Body | ConvertFrom-Json -AsHashtable

then all the variables will be available using $hash[]

e.g: $hash["$content"]

Thiago Custodio
  • 17,332
  • 6
  • 45
  • 90
  • Thanks @Thiago. I replaced `$content = $Request.Query.baz` with `$hash = $Request.Body | ConvertFrom-Json -AsHashtable`. Will you advise on how to complete the `run.ps1` file with the correct substitutions? – ericOnline Oct 10 '19 at 20:22
  • what else you azure function needs to do? – Thiago Custodio Oct 10 '19 at 21:16
  • Nothing, just prettify the JSON. My understanding is that PowerShell automatically prettifies serial JSON when its opened. I'm not even sure if ConvertFrom/To-JSON is required. – ericOnline Oct 11 '19 at 15:33
  • https://stackoverflow.com/questions/24789365/prettify-json-in-powershell-3 – Thiago Custodio Oct 11 '19 at 16:23
  • I've seen this. It only addresses opening a STRING of json in local instance of PowerShell. I need to know the syntax for interacting with a FILE of json in an Azure Function. – ericOnline Oct 11 '19 at 17:39
  • all you need to do is replace the string to the content (which will contain your file). | $Request.Body | ConvertFrom-Json | ConvertTo-Json – Thiago Custodio Oct 11 '19 at 18:20