HttpTrigger Powershell云函数返回text/plain而不是text/html

问题描述 投票:0回答:1

我创建了这个powershell函数:

function.json

{
  "bindings": [
    {
      "authLevel": "anonymous",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    }
  ]
}

运行.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>
"@
$response = @{
    StatusCode = 200
    Body = $htmlContent
    Headers = @{
        "Content-Type" = "text/html"
    }
}
Push-OutputBinding -Name res -Value ($response)

我打算使用这样的东西来动态生成网页 但下面命令的输出显示 ContentType 是 text/plain,我需要它是 text/html

curl -v -X GET“http://localhost:7071/api/htmlpage”

Note: Unnecessary use of -X or --request, GET is already inferred.
*   Trying 127.0.0.1:7071...
* Connected to localhost (127.0.0.1) port 7071 (#0)
> GET /api/htmlpage HTTP/1.1
> Host: localhost:7071
> User-Agent: curl/7.81.0
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Content-Type: text/plain; charset=utf-8
< Date: Sun, 07 Apr 2024 10:54:00 GMT
< Server: Kestrel
< Transfer-Encoding: chunked
< 
<!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>
* Connection #0 to host localhost left intact

我正在使用 linux powershell 运行时 有可能实现这个工作吗?

azure powershell azure-functions
1个回答
0
投票

下面是对我有用的

script

using namespace System.Net
param($Request, $TriggerMetadata)

$htmlContent = @"
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Rithwik Test Page</title>
</head>
<style>
h1 {
  color: blue;
  text-indent: 20px;
  text-transform: uppercase;
}
</style>
<body>
    <h1>Hello from Azure Functions!!!</h1>
    <h1>Rithwik Bojja how are you!!!!!</h1>
</body>
</html>
"@

Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
    StatusCode = [HttpStatusCode]::OK
    Body = $htmlContent
    Headers =@{'content-type'='text\html'}
    })

我使用内容类型为“text\html”。

function.json:

{
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "Request",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "Response"
    }
  ]
}

Output:

enter image description here

使用卷曲:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.