如何从客户端应用程序调用Azure函数并添加一个或多个函数参数? [英] How can I call an Azure function from the client application and add one or more function parameters?

查看:61
本文介绍了如何从客户端应用程序调用Azure函数并添加一个或多个函数参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从客户端应用程序调用此Azure函数,但是我不知道如何执行http请求.

I want to call this Azure function from the client application but I don't know how to do the http request.

天蓝色功能

    [FunctionName("ChangeDisplayname")]
    public static async Task<dynamic> MakeApiCall(
        [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequestMessage req, ILogger log)
    {
        var context = await FunctionContext<dynamic>.Create(req);
        var args = context.FunctionArgument;

        var desireddisplayname = args["NewDisplayname"];

        var request = new UpdateUserTitleDisplayNameRequest();
        request.PlayFabId = context.CallerEntityProfile.Lineage.MasterPlayerAccountId;
        request.DisplayName = desireddisplayname;
       
        var adminApi = new PlayFabAdminInstanceAPI(context.ApiSettings, context.AuthenticationContext);

        return await adminApi.UpdateUserTitleDisplayNameAsync(request);
    }

这是我的客户端应用程序代码,用于调用Azure函数.但这是行不通的,因为我不知道如何将所需的显示名称添加到http请求中.所需的显示名称应为功能参数"NewDisplayname".例如,如果期望的显示名称=克里斯",则var期望的显示名称应具有相同的值克里斯".当我调用Azure函数时.

This is my client application code to call the Azure function. But it is not working because I don't know how to add desireddisplayname to the http request. desireddisplayname should be the function parameter "NewDisplayname". For example, if desireddisplayname = "Chris", then var desireddisplayname should have the same value "Chris" when I call the Azure function.

使用await _httpClient.GetAsync(url)时是否可以添加所需的显示名称?

Is it somehow possible to add desireddisplayname when I use await _httpClient.GetAsync(url)?

如何从客户端应用程序调用Azure函数并添加一个或多个函数参数?

How can I call an Azure function from the client application and add one or more function parameters?

    public static async Task<(bool requestexecuted, string desireddisplayname, string errormessage)> Azurehttprequest(this string url)
    {
        bool requestexecuted = false;
        string errormessage = string.Empty;

        var _httpClient = new HttpClient { Timeout = TimeSpan.FromSeconds(15) };

        try
        {
            using (var httpResponse = await _httpClient.GetAsync(url))
            {
                if (httpResponse.StatusCode == HttpStatusCode.OK)
                {
                    requestexecuted = true;
                }
                else
                {
                    requestexecuted = false;
                }
            }
        }
        catch (Exception)
        {
            requestexecuted = false;
        }

        return (requestexecuted, errormessage);
    }

推荐答案

由于您的功能是-您必须使用 HTTP POST 方法而不是 HTTP GET 来调用此函数的端点.

As your function is an HttpTrigger and is HTTP POST endpoint as per [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] - you will have to call this function's Endpoint with HTTP POST method and not HTTP GET.

下面是您可能要使用的代码:

Below is the code you might want to use instead:

客户:

using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, requestUri))
            {
                var body = new RequestModel
                {
                    NewDisplayname = desireddisplayname
                };

                request.Content = new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json");

                    HttpResponseMessage responseMsg = await httpClient.SendAsync(request).ConfigureAwait(false);

                    if (responseMsg == null)
                    {
                        throw new InvalidOperationException(
                            string.Format(
                                "The response message was null when executing operation {0}.",
                                request.Method));
                    }

                    return responseMsg;
                }

要读取的功能代码:

// Get request body
                RequestModel data = await req.Content.ReadAsAsync<RequestModel>();
                name = data.NewDisplayname;

这篇关于如何从客户端应用程序调用Azure函数并添加一个或多个函数参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆