如何使用POST请求中的参数触发Azure函数

ukdjmx9f  于 2022-12-14  发布在  其他
关注(0)|答案(3)|浏览(129)

我需要向Azure函数发送POST请求(带有用户捕获的数据),我希望Azure函数将此请求用作参数并运行。
我可以看到有很多方法来触发文档中的Azure功能,但是我还没有能够弄清楚如何在提供用户输入时做到这一点。
对于更多的上下文,我正在开发一个简单的用户表单,用户在其中输入一些数据。一旦用户点击提交,我将需要向Azure函数发出POST请求来执行一些处理。

q7solyqu

q7solyqu1#

下面是HTTP POST请求触发的Function App的示例代码。

一月一日

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "$return"
    }
  ]
}

一个月一个月

import logging
import azure.functions as func
import onnxruntime
from PIL import Image
import numpy as np
import io

def main(req: func.HttpRequest, context: func.Context) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    body = req.get_body()

    try:
        image = Image.open(io.BytesIO(body))
    except IOError:
        return func.HttpResponse(
                "Bad input. Unable to cast request body to an image format.",
                status_code=400
        )

    result = run_inference(image, context)

    return func.HttpResponse(result)

def run_inference(image, context):
    # See https://github.com/onnx/models/tree/master/vision/style_transfer/fast_neural_style
    # for implementation details
    model_path = f'{context.function_directory}/rain_princess.onnx'
    session = onnxruntime.InferenceSession(model_path)
    metadata = session.get_modelmeta()
    logging.info(f'Model metadata:\n' +
        f'    Graph name: {metadata.graph_name}\n' +
        f'    Model version: {metadata.version}\n' +
        f'    Producer: {metadata.producer_name}')

    # Preprocess image
    original_image_size = image.size[0], image.size[1]
    logging.info('Preprocessing image...')
    # Model expects a 224x224 shape input
    image = image.resize((224, 224), Image.LANCZOS)
    bands = image.getbands()
    if bands == ('R', 'G', 'B'):
        logging.info(f'Image is RGB. No conversion necessary.')
    else:
        logging.info(f'Image is {bands}, converting to RGB...')
        image = image.convert('RGB')

    x = np.array(image).astype('float32')
    x = np.transpose(x, [2, 0, 1])
    x = np.expand_dims(x, axis=0)

    output_name = session.get_outputs()[0].name
    input_name = session.get_inputs()[0].name
    logging.info('Running inference on ONNX model...')
    result = session.run([output_name], {input_name: x})[0][0]

    # Postprocess image
    result = np.clip(result, 0, 255)
    result = result.transpose(1,2,0).astype("uint8")
    img = Image.fromarray(result)
    max_width  = 800
    height = int(max_width * original_image_size[1] / original_image_size[0])
    # Upsample and correct aspect ratio for final image
    img = img.resize((max_width, height), Image.BICUBIC)
    
    # Store inferred image as in memory byte array
    img_byte_arr = io.BytesIO()
    # Convert composite to RGB so we can return JPEG
    img.convert('RGB').save(img_byte_arr, format='JPEG')
    final_image = img_byte_arr.getvalue()

    return final_image

看看这个Python POST请求的GitHub示例:https://github.com/yokawasa/azure-functions-python-samples/tree/master/v2functions/http-trigger-onnx-model

wrrgggsh

wrrgggsh2#

此代码段可以工作:https://github.com/yuenci/Azure-Functions-Request-CORS

# post
import logging

import azure.functions as func
import json

def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    req_body_bytes = req.get_body()
    req_body = req_body_bytes.decode("utf-8")

    logging.info(f"Request: {req_body}")

    content = json.loads(req_body)

    first_name = content["first_name"]
    last_name = content["last_name"]

    return func.HttpResponse(
        f"Hello, {first_name} {last_name}. This HTTP triggered function executed successfully.",
        status_code=200
    )

确保允许post方法

// function.json

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "authLevel": "anonymous",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get",
        "post" // notice
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "$return"
    }
  ]
}
6tr1vspr

6tr1vspr3#

提交的表单中的数据将是一个Bytes对象。您必须将其转换为字符串,然后分析参数。此示例代码处理基本联系人信息表单的提交。

import logging

import azure.functions as func

from urllib.parse import parse_qs

def main(req: func.HttpRequest) -> func.HttpResponse:
    # This function will parse the response of a form submitted using the POST method
    # The request body is a Bytes object
    # You must first decode the Bytes object to a string
    # Then you can parse the string using urllib parse_qs

    logging.info("Python HTTP trigger function processed a request.")
    req_body_bytes = req.get_body()
    logging.info(f"Request Bytes: {req_body_bytes}")
    req_body = req_body_bytes.decode("utf-8")
    logging.info(f"Request: {req_body}")

    first_name = parse_qs(req_body)["first_name"][0]
    last_name = parse_qs(req_body)["last_name"][0]
    email = parse_qs(req_body)["email"][0]
    cell_phone = parse_qs(req_body)["cell_phone"][0]

    return func.HttpResponse(
        f"You submitted this information: {first_name} {last_name} {email} 
        {cell_phone}",
        status_code=200,
    )

相关问题