Azure 函数看不到从 Shopify 正确传递的 JSON 正文

Azure function doesnt see JSON body passed from Shopify properly

我正在为 shopify 商店构建一个基于 post 结帐流程的 Azure 函数应用程序。这是我第一次在功能的约束下工作。该函数基于 powershell。

所以该功能是在本地开发的,为此,我用 hookbin 复制了 webhook 的 json 主体(Order/Paid 来自 Shopify),hookbin 是一个很棒的工具。然后,我在开发函数时使用 postman 在函数的本地副本处触发函数体。 这是 shopifys 示例的片段:

{
  "id": 820982911946154500,
  "email": "jon@doe.ca",
  "closed_at": null,
  "created_at": "2021-02-07T20:48:25+00:00",
  "updated_at": "2021-02-07T20:48:25+00:00",
  "number": 234,
  "note": null,
  ...
}

所以我使用它,看起来很简单。在我的 powershell 中,我做了一个相当标准的

$RequestBody = $Request.Body | ConvertFrom-JSON

然后我挑选我需要的东西。

$email_address = $RequestBody.email

当我将 Shopify 和功能一起加入时,这不起作用。在我收集请求正文时,我没有得到与 hookbin 相同的东西。

当我尝试转换 json 时,我得到了这个

WARNING: Conversion from JSON failed with error: Unexpected character encountered while parsing value: S. Path '', line 0, position 0.

所以我尝试将主体转储为 $Request.body(在我转换为 json 之前)并得到这个

INFORMATION: System.Collections.DictionaryEntry System.Collections.DictionaryEntry System.Collections.DictionaryEntry System.Collections.DictionaryEntry System.Collections.DictionaryEntry 

所以,总而言之,我显然没有达到我期待的严格 JSON。 这是我函数的前几行。

param($Request, $TriggerMetadata)
try {
    $ErrorActionPreference = "Stop"
    Connect-AzAccount -Identity

    # $RequestBody = $Request.Body | convertfrom-json
    Write-host $Request.Body  

我是否遗漏了一个非常明显的步骤,该步骤需要在函数中完成,而我在使用 hookbin 收集尸体时不需要这样做?

查看

的输出
Write-host $Request.Body  

输出:

INFORMATION: System.Collections.DictionaryEntry System.Collections.DictionaryEntry System.Collections.DictionaryEntry System.Collections.DictionaryEntry System.Collections.DictionaryEntry 

看来您已经有了字典对象(键​​值对)。

如果您必须访问 email 键的值,您可以直接作为 $request.body.email$request.body["email"]

访问它

在执行上述操作之前,您可以先尝试以下步骤:

了解存在哪些键。因此访问 them.So 你可以尝试做 Write-Host $request.body.Keys 来转储与字典对象关联的所有键。