-Headers:术语“-Headers”未被识别为 cmdlet 的名称,- Azure 函数 运行 在带有调用 Web 请求的 cmd 提示符中

-Headers : The term '-Headers' is not recognized as the name of a cmdlet, - Azure functions running in cmd prompt with Invoke web request

我在 VS Code[=40 中有一个 Azure 函数(堆栈:.Net - HttpTrigger 模板) =] 使用以下代码:

public static class OnPaymentReceived
    {
        [FunctionName("OnPaymentReceived")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("Received a payment.");

            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data = JsonConvert.DeserializeObject(requestBody);
           
            return new OkObjectResult($"Thank you for your purchase");
        }
    }

我知道我们可以通过多种方式 运行 Azure Function,其中我使用 IWR 通过 Windows PowerShell 使用以下 cmdlet:

iwr -Method POST `
-Uri http://localhost:7071/api/OnPaymentReceived
-Headers @{"Content-Type"="application/json"}`
-Body '{}'

我获得了成功的输出,但出现与 cmdlet 的第 3 行相关的错误:

输出:

StatusCode        : 200
StatusDescription : OK
Content           : Thank you for your purchase
RawContent        : HTTP/1.1 200 OK
                    Transfer-Encoding: chunked
                    Content-Type: text/plain; charset=utf-8
                    Date: Fri, 24 Dec 2021 01:10:33 GMT
                    Server: Kestrel

                    Thank you for your purchase
Forms             : {}
Headers           : {[Transfer-Encoding, chunked], [Content-Type, text/plain; charset=utf-8], [Date, Fri, 24 Dec 2021
                    01:10:33 GMT], [Server, Kestrel]}
Images            : {}
InputFields       : {}
Links             : {}
ParsedHtml        : mshtml.HTMLDocumentClass
RawContentLength  : 27

异常: 当我使用第三个 cmdlet 的语法时,例如:-Headers @{"Content-Type"="application/json"}

-Headers : The term '-Headers' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:3 char:1
+ -Headers @{"Content-Type"="application/json"}`
+ ~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (-Headers:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

当我使用第三个 cmdlet 的另一种语法时,例如:header('Content-type: application/json');

header : The term 'header' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:3 char:1
+ header('Content-type: application/json');
+ ~~~~~~
    + CategoryInfo          : ObjectNotFound: (header:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

-Body : The term '-Body' is not recognized as the name of a cmdlet, function, script file, or operable program. Check
the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:4 char:1
+ -Body '{}'
+ ~~~~~
    + CategoryInfo          : ObjectNotFound: (-Body:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

如何解决此异常(此 -Headers cmdlet 的正确语法是什么)?

-Headers : The term '-Headers' is not recognized as the name of a cmdlet, function, script file, or operable program.

该错误是由于cmdlet中的语法错误引起的。

iwr -Method POST `
-Uri http://localhost:7071/api/OnPaymentReceived `
-headers @{"Content-Type"="application/json"} `
-Body '{} '

每行命令后必须有一个space和撇号,格式如下 (-cmdlet cmdends ` ).

我已经用上面的 cmdlet 试过了,你可以看到没有任何错误的输出。