提前感謝您的支持和幫助!
我在VS Code 中有一個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");
}
}
我知道我們可以IWR通過多種方式運行 Azure 函式,我通過 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 的正確語法是什么)?
uj5u.com熱心網友回復:
-Headers :術語“-Headers”不被識別為 cmdlet、函式、腳本檔案或可運行程式的名稱。
該錯誤是由 cmdlet 中的語法錯誤引起的。
iwr -Method POST `
-Uri http://localhost:7071/api/OnPaymentReceived `
-headers @{"Content-Type"="application/json"} `
-Body '{} '
在每一行命令之后必須有一個空格和撇號符號,如下面的格式 (-cmdlet cmdends ` )。
我已經嘗試過上面的 cmdlet ,你可以看到沒有任何錯誤的輸出。

轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/391695.html
下一篇:為不可見面板觸發事件-.Net
