You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure Function调用出现404错误:HTTP Trigger函数配置测试问题

Azure函数HTTP触发测试404错误解决及代码适配

问题详情

在Azure门户的Function App概览页创建了新函数,配置如下:

  • 开发环境:门户内开发
  • 模板选择:HTTP Trigger
  • 函数名称:wordcount
  • 授权级别:Function

替换默认代码为以下内容并保存:

import logging
import azure.functions as func
import json
import re
from collections import Counter

# 忽略的停用词列表
STOPWORDS = set([
    '', 'i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you',
    "you're", "you've", "you'll", "you'd", 'your', 'yours', 'yourself',
    'yourselves', 'he', 'him', 'his', 'himself', 'she', "she's", 'her',
    'hers', 'herself', 'it', "it's", 'itself', 'they', 'them',
    'their', 'theirs', 'themselves', 'what', 'which', 'who', 'whom',
    'this', 'that', "that'll", 'these', 'those', 'am', 'is', 'are', 'was',
    'were', 'be', 'been', 'being', 'have', 'has', 'had', 'having', 'do',
    'does', 'did', 'doing', 'a', 'an', 'the', 'and', 'but', 'if', 'or',
    'because', 'as', 'until', 'while', 'of', 'at', 'by', 'for', 'with',
    'about', 'against', 'between', 'into', 'through', 'during', 'before',
    'after', 'above', 'below', 'to', 'from', 'up', 'down', 'in', 'out',
    'on', 'off', 'over', 'under', 'again', 'further', 'then', 'once', 'here',
    'there', 'when', 'where', 'why', 'how', 'all', 'any', 'both', 'each',
    'few', 'more', 'most', 'other', 'some', 'such', 'no', 'nor', 'not',
    'only', 'own', 'same', 'so', 'than', 'too', 'very', 'can', 'will',
    'just', "don't", 'should', "should've", 'now', "aren't", "couldn't",
    "didn't", "doesn't", "hadn't", "hasn't", "haven't", "isn't", "mightn't", "mustn't",
    "needn't", "shan't", "shouldn't", "wasn't", "weren't", "won't", "wouldn't"
])

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

    try:
        req_body = req.get_json()
        if "values" in req_body:
            vals = req_body["values"]
            res = {"values": []}

            for rec in vals:
                # 获取当前输入的记录ID和文本
                record_id = rec['recordId']
                text = rec['data'].get('text', '')

                # 移除标点和数字,转换为小写
                text = re.sub(r'[^A-Za-z\s]', '', text).lower()

                # 分割为单词数组
                words = text.split()

                # 统计非停用词的出现次数
                word_counts = Counter(word for word in words if word not in STOPWORDS)

                # 获取出现频率最高的10个单词
                top_words = [word for word, count in word_counts.most_common(10)]

                # 添加当前记录的结果
                res["values"].append({
                    "recordId": record_id,
                    "data": {
                        "text": top_words
                    }
                })

            return func.HttpResponse(
                body=json.dumps(res),
                status_code=200,
                mimetype="application/json"
            )

        else:
            return func.HttpResponse(
                body=json.dumps({"errors": [{"message": "Invalid input"}]}),
                status_code=400,
                mimetype="application/json"
            )

    except Exception as e:
        logging.error(f"Error processing request: {e}")
        return func.HttpResponse(
            body=json.dumps({"errors": [{"message": str(e)}]}),
            status_code=500,
            mimetype="application/json"
        )

在「测试/运行」面板使用以下JSON请求体测试时,出现404错误:

{
    "values": [
        {
            "recordId": "a1",
            "data":
            {
            "text":  "Tiger, tiger burning bright in the darkness of the night.",
            "language": "en"
            }
        },
        {
            "recordId": "a2",
            "data":
            {
            "text":  "The rain in spain stays mainly in the plains! That's where you'll find the rain!",
            "language": "en"
            }
        }
    ]
}

期望返回结果格式:

{
"values": [
    {
    "recordId": "a1",
    "data": {
        "text": [
        "tiger",
        "burning",
        "bright",
        "darkness",
        "night"
        ]
    },
    "errors": null,
    "warnings": null
    },
    {
    "recordId": "a2",
    "data": {
        "text": [
        "rain",
        "spain",
        "stays",
        "mainly",
        "plains",
        "thats",
        "youll",
        "find"
        ]
    },
    "errors": null,
    "warnings": null
    }
]
}

404错误解决步骤

  1. 确认部署状态:保存代码后等待门户顶部显示「已成功部署函数」提示,再执行测试。
  2. 检查测试请求配置:
    • 确保请求方法为POST
    • 验证请求URL格式:https://<你的Function App名称>.azurewebsites.net/api/wordcount
    • 因授权级别为Function,需在请求头添加x-functions-key,值为函数概览页的默认密钥。
  3. 核对路由规则:门户创建的HTTP Trigger默认路由为函数名,无需额外修改。

代码调整适配期望输出

当前返回结果缺少errors和warnings字段,需修改结果追加部分的代码:

将原代码中的:

res["values"].append({
    "recordId": record_id,
    "data": {
        "text": top_words
    }
})

替换为:

res["values"].append({
    "recordId": record_id,
    "data": {
        "text": top_words
    },
    "errors": null,
    "warnings": null
})

注:原代码处理标点时会将That's转为thats、you'll转为youll,与期望结果一致,无需额外修改。

内容的提问来源于stack exchange,提问作者Techzill

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 02:17:18