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

Linux环境PowerShell HttpTrigger函数返回text/plain而非text/html问题

解决Linux PowerShell Azure Functions返回Content-Type为text/plain的问题

针对你遇到的问题,有两种可靠的解决方式:

方案一:使用HttpResponseContext对象构建响应(推荐)

直接使用Azure Functions Worker提供的HttpResponseContext类来构建响应,能确保响应头在Linux运行时被正确识别:

修改run.ps1代码如下:

param($req, $TriggerMetadata)

$htmlContent = @"
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Sample Page</title>
</head>
<body>
    <h1>Hello from Azure Functions!</h1>
</body>
</html>
"@

# 初始化HttpResponseContext对象并配置响应
$response = [Microsoft.Azure.Functions.Worker.Http.HttpResponseContext]::new()
$response.StatusCode = [System.Net.HttpStatusCode]::OK
$response.Headers.Add("Content-Type", "text/html; charset=utf-8")
$response.WriteString($htmlContent)

Push-OutputBinding -Name res -Value $response

方案二:调整哈希表的Headers格式(兼容写法)

如果偏好使用哈希表,可尝试显式指定字符编码,确保Headers的格式被运行时正确解析:

param($req, $TriggerMetadata)
$htmlContent = @"
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Sample Page</title>
</head>
<body>
    <h1>Hello from Azure Functions!</h1>
</body>
</html>
"@
$response = @{
    StatusCode = 200
    Body = $htmlContent
    Headers = @{
        "Content-Type" = "text/html; charset=utf-8"
    }
}
Push-OutputBinding -Name res -Value $response

问题原因

在Linux PowerShell运行时环境中,直接传递简单哈希表作为响应值时,运行时对Headers的解析存在兼容性差异,导致Content-Type未被正确设置为text/html。使用官方提供的HttpResponseContext对象可规避这类解析问题,确保响应头按预期生效。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 15:07:02