skip to Main Content

I’m trying to write a few azure functions which will do some reasonably simple integrations after a Shopify order sends a webhook.

I’ve got a conceptual issue that I’d appreciate some help with.

I’m trying to mock up the webhook in Postman. to do this, I used Hookbin to collect the output from Shopify and the body came through a little like this

{
  "id": 3207753990315,
  "email": "[email protected]",
  "closed_at": null,
  "created_at": "2021-02-02T21:44:18+00:00",
  "updated_at": "2021-02-02T21:44:19+00:00",
  "number": 28,
  "note": "",
  "total_price": "10.00",
  ....
}

I think that this is json formatted.
so i took this from hookbin and put it into the body (raw) of a postman call. using the local address for the azure function. (which i’m coding in VS Code on a mac). When i send this it works fine. In the function i can collect the $Request.Body and convert from JSON format.

The problem comes in when i promote my function to azure.
An Azure Function receives a custom object in as $Request, so the convert from JSON line isnt needed.

I need to understand how to mock the JSON that ive got from Hookbin up into the same format as the object that the function receives as this would speed up my dev flow loads. and just be a better pattern to be fair. I’m realitvely sure that this is the step I’ve got wrong. In collecting the payload with Hookbin, I’ve got the JSON format of the order, but thats not how its received in an Azure Function, which according to the documention presents the $Request as on object.

2

Answers


  1. $Request.RawBody may give you what you need.

    Login or Signup to reply.
  2. The beauty of the Azure Functions host is that you don’t need to worry about converting the JSON yourself (depending on how your Postman request is constructed.

    For example here I am using POST method to send to my function with a JSON payload.

    Insomnia API POST Example to Azure Functions running locally

    If a POST method is used against your function then that will automatically be populated in $Request.Body whereas if you use GET it will automatically populate $Request.Query

    The JSON object is serialised for you so that you don’t need to convert from JSON inside your function.

    My function looks like below:

    using namespace System.Net
    
    # Input bindings are passed in via param block.
    param (
        $Request,
        $TriggerMetadata
    )
    
    # Interact with query parameters or the body of the request.
    $deserializedObject = $Request.Body
    
    # Associate values to output bindings by calling 'Push-OutputBinding'.
    Push-OutputBinding -Name Response -Value (
        [HttpResponseContext]@{
            StatusCode = [HttpStatusCode]::OK
            Body = $deserializedObject
        }
    )
    

    And debugging that function to inspect the value as it’s passed in you can see that it’s automatically converted to a hashtable without me having to call ConvertFrom-Json inside my function.

    Debugging Azure function locally in VSCode

    And you can see that I didn’t have to use ConvertTo-Json to output my results to Push-OutputBinding either and the returned value in Insomnia is JSON.

    Output of calling local Azure Function inside Insomnia Application

    I am not sure how you have your local environment setup for testing but using the Function Core Tools should allow you to run and execute the functions locally and it should be the same as it is executed in Azure.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search