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

こ雲淡風輕ζ 提交于 2020-12-15 06:25:06

问题


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

Azure function:

    [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);
    }

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.

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

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);
    }

回答1:


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:

Client:

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;
                }

Function code to read:

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


来源:https://stackoverflow.com/questions/64027552/how-can-i-call-an-azure-function-from-the-client-application-and-add-one-or-more

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!