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
$Request.RawBody
may give you what you need.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.
If a
POST
method is used against your function then that will automatically be populated in$Request.Body
whereas if you useGET
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:
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.And you can see that I didn’t have to use
ConvertTo-Json
to output my results toPush-OutputBinding
either and the returned value in Insomnia is JSON.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.