
本文旨在解决 azure function 中处理 http 请求时可能遇到的“unexpected end of request content”错误。通过详细阐述如何优化请求体解析机制,避免 `req.get_json()` 潜在的隐患,并引入 `req.get_body()` 结合显式 json 解析及 `incompleteread` 异常处理,从而提升函数的健壮性和错误处理能力,确保在接收不完整或格式异常的请求时能够优雅地响应。
在开发 Azure Function 处理 HTTP 请求,特别是接收 Webhook 数据时,开发者可能会遇到日志中出现“Unexpected end of request content”的错误。这个错误通常发生在函数尝试读取或解析 HTTP 请求体时,表明请求内容在预期结束之前就中断了,导致解析失败。这可能是由于客户端发送了不完整的请求、网络中断或服务器端在读取请求流时遇到了问题。
理解 req.get_json() 的局限性
Azure Functions 提供的 func.HttpRequest 对象包含 get_json() 方法,它旨在方便地将请求体解析为 JSON 对象。然而,当请求体不完整或格式不正确时,get_json() 方法可能会在内部抛出异常,并且这些异常可能不会被外部的 try-except 块有效捕获,或者会转化为更通用的“Unexpected end of request content”日志信息,使得问题难以定位和处理。
为了更精细地控制请求体的读取和解析过程,并捕获更多类型的潜在错误,建议避免直接使用 req.get_json(),而是采用分步处理的方式。
健壮的请求体处理策略
解决此问题的核心在于显式地获取原始请求体内容,然后手动进行 JSON 解析,并在此过程中捕获可能出现的特定异常。
- 获取原始请求体: 使用 req.get_body() 方法获取原始的字节流数据。
- 检查请求体完整性: 在尝试解析之前,检查获取到的字节流是否为空或长度为零。
- 显式 JSON 解析: 使用 Python 标准库 json.loads() 将字节流解析为 JSON 对象。
- 捕获特定异常: 在解析过程中,除了通用的 Exception 外,还应捕获 ValueError(针对 JSON 格式错误)和 http.client.IncompleteRead(针对不完整的请求内容)。
以下是改进后的请求体处理逻辑示例:
import os
import base64
import json
import jwt
from hashlib import md5
from datetime import datetime, timedelta
import azure.functions as func
import logging
import requests
from http.client import IncompleteRead # 导入 IncompleteRead 异常
def webhook_decoder_baas(req: func.HttpRequest) -> func.HttpResponse:
try:
# ... (省略私钥、公钥等初始化代码)
private_key = os.environ["private_key_baas"]
private_key_baas = base64.b64decode(private_key).decode("utf-8")
qi_public_key = '''-----BEGIN PUBLIC KEY-----
... :)
-----END PUBLIC KEY-----'''
authorization = req.headers.get("AUTHORIZATION")
if not authorization:
logging.error("AUTHORIZATION header not provided.")
return func.HttpResponse("AUTHORIZATION header not provided.", status_code=400)
# 优化请求体处理部分
try:
body = req.get_body() # 获取原始字节流
if body is None or len(body) == 0:
logging.error("Empty or missing request body.")
return func.HttpResponse("Empty or missing request body.", status_code=400)
body_json = json.loads(body) # 显式解析 JSON
except ValueError as json_error:
# 捕获 JSON 解析错误
logging.error(f"Error parsing JSON: {str(json_error)}")
return func.HttpResponse("Error parsing JSON", status_code=400)
except IncompleteRead as incomplete_read_error:
# 捕获请求内容不完整错误
logging.error(f"Incomplete read error: {str(incomplete_read_error)}")
return func.HttpResponse("Incomplete read error", status_code=400)
# JWT 解码和断言验证
decoded_header = jwt.decode(token=authorization, key=qi_public_key)
# 使用断言进行数据验证,并提供清晰的错误信息
assert decoded_header.get("method") == "POST", "Error in the sending method, a different method than POST was used."
assert decoded_header.get("payload_md5") == md5(json.dumps(body_json).encode()).hexdigest(), "Payload hash does not match the expected one."
assert (datetime.utcnow() - timedelta(minutes=5)) < datetime.strptime(decoded_header.get("timestamp"), "%Y-%m-%dT%H:%M:%S.%fZ") < (datetime.utcnow() + timedelta(minutes=5)), "Error in sending timestamp, outside the 10-minute limit."
# ... (原有业务逻辑:数据提取、创建新 JSON、发送 POST 请求等)
payload_string = json.dumps(body_json)
objeto_payload = json.loads(payload_string) # 这一步实际上是多余的,body_json已经是Python对象
if (
objeto_payload.get("webhook_type") == "bank_slip.status_change" and
(objeto_payload.get("status") == "payment_notice" or objeto_payload.get("status") == "notary_office_payment_notice")
):
bank_slip_key = objeto_payload.get("data", {}).get("bank_slip_key")
paid_amount = objeto_payload.get("data", {}).get("paid_amount")
payload_output = {
key: objeto_payload[key] for key in ["status"]
}
payload_output['Paid amount'] = paid_amount
payload_output['Query key of the title'] = bank_slip_key
url = "https://nerowebhooks.azurewebsites.net/api/information/send"
token = jwt.encode(payload_output, private_key_baas, algorithm='ES512')
headers = {'SIGNATURE': token}
response = requests.post(url, headers=headers, json=payload_output)
logging.info(f"Received webhook: {payload_output}")
return func.HttpResponse("Webhook received successfully!", status_code=200)
else:
logging.info("Webhook received successfully, but it won't be handled at the moment!")
return func.HttpResponse("Webhook received successfully, but it won't be handled at the moment!")
# 集中处理 JWT 相关的错误
except jwt.JWTError as jwt_error:
logging.error(f"Token error: {str(jwt_error)}")
return func.HttpResponse(f"Token error: {str(jwt_error)}", status_code=400)
# 集中处理断言错误
except AssertionError as assertion_error:
logging.error(f"Assertion error: {str(assertion_error)}")
return func.HttpResponse(str(assertion_error), status_code=400)
# 捕获所有其他未预料的内部错误
except Exception as e:
logging.error(f"Internal error while processing the webhook: {str(e)}")
return func.HttpResponse(f"Internal error while processing the webhook: {str(e)}", status_code=500)
代码改进点说明:
- req.get_body() 与 json.loads() 结合: 这是解决“Unexpected end of request content”错误的关键。get_body() 获取原始字节流,json.loads() 负责解析。
- IncompleteRead 异常捕获: http.client.IncompleteRead 异常专门用于处理 HTTP 协议中请求体不完整的情况,通过捕获此异常,可以更明确地识别并响应这类问题。
- ValueError 异常捕获: 针对 JSON 格式本身不正确的情况,json.loads() 会抛出 ValueError,捕获此异常可以区分是 JSON 格式问题还是请求体不完整问题。
- 提前检查请求体: 在解析 JSON 之前,先检查 body 是否为空,可以避免在空请求体上尝试解析 JSON。
- 统一错误处理: 将 JWT 错误和断言错误分别捕获,并返回相应的 HTTP 状态码(如 400 Bad Request),使错误信息更具体。通用的 Exception 作为最后的兜底,捕获所有未预期的内部错误并返回 500 Internal Server Error。
- 精简 JSON 转换: 原始代码中 payload_string = json.dumps(body) 和 objeto_payload = json.loads(payload_string) 实际上是将 body_json (已经是 Python 对象)再次序列化和反序列化。在新的逻辑中,body_json 已经是一个 Python 对象,可以直接使用。
注意事项与最佳实践
- HTTP 状态码: 针对不同类型的错误返回合适的 HTTP 状态码至关重要。例如,客户端请求格式错误(如 JSON 解析失败、授权头缺失)应返回 400 Bad Request;认证或授权失败返回 401 Unauthorized 或 403 Forbidden;服务器内部错误返回 500 Internal Server Error。
- 详细日志: 在 try-except 块中记录详细的错误信息(包括异常类型和消息),有助于在生产环境中快速定位问题。
- 幂等性: 如果 Webhook 处理涉及状态变更,请考虑实现幂等性,以防客户端因网络问题重试发送请求。
- 异步处理: 对于耗时较长的 Webhook 处理,可以考虑将核心业务逻辑放入消息队列(如 Azure Service Bus 或 Azure Queue Storage),由另一个 Azure Function 异步处理,从而快速响应 Webhook 请求,避免超时。
- 安全性: 确保所有敏感信息(如私钥)都通过环境变量安全管理,并对传入数据进行严格的验证和净化。
通过上述优化,您的 Azure Function 将能更健壮地处理各种 HTTP 请求,特别是那些可能存在内容不完整或格式异常的 Webhook,显著提高服务的稳定性和可靠性。










