接收pdf文件时,Azure Functions Http触发器,然后使用Form Recognizer [python]分析表单

时间:2020-06-08 07:13:22

标签: python-3.x azure function

因此,以下工作流程:

  1. 一位同事在我的Azure Functions端点上发送交货收据(pdf或图像)。
  2. Azure函数使用HTTP触发器接收文件。
  3. 然后该文件将由FOrm Recognizer(Microsoft认知服务)进行分析
  4. 以Json的形式发送回复。

由于我是新手,所以遇到了一些问题。这是我的代码现在的样子:

import logging 
import requests
import azure.functions as func
import json

SUPPORTED_CONTENT_TYPES = ['application/pdf', 'image/jpeg', 'image/png']

# Define Form Recognizer Dependencies

apim_key = "key"
model_id = "formrecognizer model id"
formrecendpoint ="endpoint"

def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Function processed a Request')

    # recieve the file
    file = req.files.get('file')

    body = None

    # Analyze Forms Endpoint  
    endpoint = '......cognitive.microsoft.com/formrecognizer/v2.0-preview/cutom/models/modelID/analyze'

    # Analyze Form
    response = requests.post(endpoint, data = file, headers = {"Content-Type": '<file type>',"Ocp-Apim-Subscription-Key": apim_key})
    body = response.content

    #Return Response
    return func.HttpResponse(body,headers={'Content-Type':'application/json'})

但这根本不起作用。

尝试通过邮递员投递文件时出现以下错误(form-data,binary)

{
    "error": {
        "code": "1002",
        "message": "Analyze request is either invalid or missing required parameters. Refer to the API reference and retry your request."
    }
}

但是在函数本身中,我得到STATUS: 200

0 个答案:

没有答案