gpt4 book ai didi

c# - 通过 HttpTrigger 将值/参数传递给 Azure Powershell 函数

转载 作者:行者123 更新时间:2023-12-02 23:42:44 24 4
gpt4 key购买 nike

我正在尝试从 C# 代码调用由 HTTP URL 触发的 Azure 函数(PowerShell 脚本)。我想从代码中将一些值/参数传递给 Azure 函数。

理想情况下,我想将一些用户信息传递给 Azure 函数,并且该函数将使用参数运行 Connect-PnPOnline。到目前为止,Webhook 可以工作,但不知何故我找不到读取从代码传递的数据的方法。或者当我尝试发送数据时可能出了问题。

我的 C# 代码:

string theUrl = "https://xxxxxx.azurewebsites.net/api/xxxx?code=xxxx==";

var userInfo = new
{
userId = $"ooooo",
userPassword = $"xxxxxxxx"
};

HttpClient client = new HttpClient();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, new Uri(theUrl));
HttpResponseMessage response = await client.PostAsJsonAsync(theUrl, userInfo);

Powershell 脚本:

param (
[string]$userId,
[string]$userPassword
)
...

如何读取传递给 Azure 函数的对象?或者是否有更好的方法将数据传递到 Azure PowerShell 函数?

谢谢!

最佳答案

C#代码正确地在POST的请求方法主体中传递参数。

现在,为了读取 Azure Function 中的参数,默认情况下所有参数都应由 $Request 引用。

# Input bindings are passed in via param block.
param($Request, $TriggerMetadata)

读取参数如下:

# Interact with query parameters or the body of the request.
$userId = $Request.Query.userId # Query based parameters ()
if (-not $userId) {$userId = $Request.Body.userId} # JSON Body (POST)

$userPassword = $Request.Query.userPassword # Query based parameters ()
if (-not $userPassword) {$userId = $Request.Body.userPassword} # JSON Body (POST)

一旦获得参数,您就可以根据您的要求在 PowerShell 中使用它。

有关 MSDN 的更多详细信息

关于c# - 通过 HttpTrigger 将值/参数传递给 Azure Powershell 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57712954/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com