Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Azure functions http trigger Unity3d 2019.3

Discussion in 'Multiplayer' started by RendergonPolygons, Mar 3, 2020.

  1. RendergonPolygons

    RendergonPolygons

    Joined:
    Oct 9, 2019
    Posts:
    98
    I've created a test HTTP Trigger azure function. I can trigger it correctly on azure portal and browser. However trigger from Unity editor gives "Error HTTP/1.1 500 Internal Server Error".

    starting azure function:
    Code (CSharp):
    1. public static async Task<IActionResult> Run(HttpRequest req, ILogger log){
    2.     log.LogInformation("C# HTTP trigger function processed a request.");
    3.  
    4.     string name = req.Query["name"];
    5.  
    6.     string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
    7.     dynamic data = JsonConvert.DeserializeObject(requestBody);
    8.     name = name ?? data?.name;
    9.  
    10.     return name != null
    11.         ? (ActionResult)new OkObjectResult($"Hello, {name}")
    12.         : new BadRequestObjectResult("Please pass a name on the query string or in the request body");}
    My Unity code:
    Code (CSharp):
    1. formData.Add(new MultipartFormDataSection("name", "SampleName", "text/plain"));
    2.     UnityWebRequest www = UnityWebRequest.Post("https://samplefunction.azurewebsites.net/api/HttpTriggerTest?herelongblob", formData);
    3.     yield return www.SendWebRequest();
    Azure CORS configuration: Request Credentials ON: Enable Access-Control-Allow-Credentials. Function is setup always on. Integrate-Trigger: selected methods GET, POST. Authorisation level:Function.
    Code (CSharp):
    1. function's host.json: "version": "2.0", "extensionBundle": { "id": "Microsoft.Azure.Functions.ExtensionBundle", "version": "[1.*, 2.0.0)"
    App Service authentication:Anonymous

    Unity-Azure sdk and google setup search results seems all outdated/not supported :( What route should I take to get this to work please? happy to try any sdk / asset store to reach azure you may suggest! Cheers!
     
    Last edited: Mar 3, 2020
  2. voncarp

    voncarp

    Joined:
    Jan 3, 2012
    Posts:
    187
    This will get you a response based on that specific Trigger Function. You will need to set your Authorization level to "Anonymous". You also need to click the integrate tab and check the method for PUT.

    From the docs: https://docs.unity3d.com/ScriptReference/Networking.UnityWebRequest.Put.html

    Code (CSharp):
    1. using System.Collections;
    2.  
    3. using UnityEngine;
    4. using UnityEngine.Networking;
    5.  
    6. public class TestAzure : MonoBehaviour
    7. {
    8.     void Start()
    9.     {
    10.         StartCoroutine(Upload());
    11.     }
    12.  
    13.     class Name
    14.     {
    15.         public string name;
    16.  
    17.         public Name(string nameToInit)
    18.         {
    19.             name = nameToInit;
    20.         }
    21.     }
    22.  
    23.     IEnumerator Upload()
    24.     {
    25.         string data = JsonUtility.ToJson(new Name("Sample Name"));
    26.         using (UnityWebRequest www = UnityWebRequest.Put("your function url goes here", data))
    27.         {
    28.             yield return www.Send();
    29.  
    30.             if (www.isNetworkError || www.isHttpError)
    31.             {
    32.                 Debug.Log(www.error);
    33.             }
    34.             else
    35.             {
    36.                 Debug.Log(www.downloadHandler.text);
    37.             }
    38.         }
    39.     }
    40. }
     
    RendergonPolygons likes this.
  3. RendergonPolygons

    RendergonPolygons

    Joined:
    Oct 9, 2019
    Posts:
    98