【问题标题】:Python Azure function with http trigger not seeing json body带有http触发器的Python Azure函数看不到json正文
【发布时间】:2021-05-19 06:18:24
【问题描述】:

我正在本地构建一个 Blazor 服务器应用程序,该应用程序调用一个用 python 编写的天蓝色函数。我正在我的本地机器上使用 Visual Studio 开发 Blazor 应用程序和 VS 代码来开发 python 函数。 Python 是 3.8.7

Blazor 应用使用 PostAsJsonAsync 作为正文中的 json 数据将数据发送到位于 http://localhost:7071/api/xxxxx 的 azure 函数。我已经使用 webhook.site 测试了它的工作原理。 JSON 数据(主要)是一个 base64 编码的 .wav 文件。

python azure 函数似乎可以看到对 PostAsJsonAsync 的调用,并且“有点”工作,就好像我在调用中添加了一个参数一样,我可以读取它。然而,python 函数总是将主体报告为长度为零。

我做错了什么?

【问题讨论】:

  • 下面的答案有帮助吗?

标签: python azure-functions blazor-server-side azure-http-trigger


【解决方案1】:
  • 检查您是否发送如下请求:

     var modelNew = new Model() { Description = "willekeurige klant", Name = "John Doe" };                     response = await client.PostAsJsonAsync("api/ModelsApi/", modelNew); 
    
     if (response.IsSuccessStatusCode) //check is response succeeded
     {
                       // Do something
     }
    
  • 并且,如下阅读:

    import logging
    import azure.functions as func
    
    
    def main(req: func.HttpRequest) -> func.HttpResponse:
      logging.info('Python HTTP trigger function processed a request.')
    
      name = req.params.get('name')
      if not name:
          try:
              req_body = req.get_json()
          except ValueError:
              pass
          else:
              name = req_body.get('name')
    
      if name:
          return func.HttpResponse(f"Hello {name}!")
      else:
          return func.HttpResponse(
              "Please pass a name on the query string or in the request body",
              status_code=400
          )
    
  • 检查您是否使用此代码对其进行编码:

      private static string Base64Encode(string plainText)
      {
          var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
          return System.Convert.ToBase64String(plainTextBytes);
      }
    

此外,请考虑在 Azure Blob 存储中使用 .wav 之类的文件,并传递它的 url 位置而不是整个对象以提高安全性。

【讨论】:

    猜你喜欢
    • 2018-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-25
    • 1970-01-01
    • 2021-11-30
    • 1970-01-01
    • 2020-01-09
    相关资源
    最近更新 更多