question

Arnau Castillo avatar image
Arnau Castillo asked

Really simple Azure Function not working.

I have created this azure function:

namespace FuncionRankingPrueba2
{
    public static class Function2
    {
        [FunctionName("FunctionPrueba2")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");


            string name = req.Query["name"];


            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data = JsonConvert.DeserializeObject(requestBody);
            name = name ?? data?.name;


            string responseMessage = string.IsNullOrEmpty(name)
                ? " Pass a name in the query string or in the request body"
                : $"Hello, {name}. This HTTP triggered function executed successfully.";


            return new OkObjectResult(responseMessage);
        }
    }
}

And I am calling the azure function from visual studio (using Unity) with this code:

public class TestLeaderboardUpdate : MonoBehaviour
{
    public void UpdateLeaderBoard()
    {
        PlayFabCloudScriptAPI.ExecuteFunction(new ExecuteFunctionRequest()
        {
            Entity = new PlayFab.CloudScriptModels.EntityKey()
            {
                Id = PlayFabSettings.staticPlayer.EntityId, 
                Type = PlayFabSettings.staticPlayer.EntityType, 
            },
            FunctionName = "Prueba2Arnau", 
            FunctionParameter =  {name= "Arnau"},
            GeneratePlayStreamEvent = false 
        }, (ExecuteFunctionResult result) =>
        {
            Debug.Log($"Result: {result.FunctionResult.ToString()}");
        }, (PlayFabError error) =>
        {
            Debug.Log($"Opps Something went wrong: {error.GenerateErrorReport()}");
        });
    }
}

I would like the result to be:

"Hello, Arnau. This HTTP triggered function executed successfully.". For some reason the name is empty and the result is "Pass a name in the query string or in the request body". Why doesn't the azure function detects the parameter "name"?

Player DataLeaderboards and Statisticsdata
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Made Wang avatar image
Made Wang answered

When calling ExecuteFunction, PlayFab does not pass FunctionParameter to Azure Function like normal Http request, but sets up a context containing the passed parameters, you can refer to CloudScriptAzureFunctionsHTTPRequestError when attempting to execute Azure function - Playfab Community to learn more.

You can refer to the following code to further improve:

public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");
            string name = req.Query["name"];
            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            log.LogInformation($"RequestBody: {requestBody}");
            dynamic data = JsonConvert.DeserializeObject(requestBody);
            name = name ?? data?.FunctionArgument?.name;
            return name != null
                ? (ActionResult)new OkObjectResult($"Hello, {name}")
                : new BadRequestObjectResult("Please pass a name on the query string or in the request body");
        }
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Igor Muhachev avatar image
Igor Muhachev answered
@Arnau Castillo Mur

Try to use:

data?.FunctionArgument?.name
,

@Arnau Castillo Mur Try to use: data?.FunctionArgument?.name

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.