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.

1 Answer 1

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"]

Sign up to request clarification or add additional context in comments.

6 Comments

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?
what else you azure function needs to do?
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.
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.