I am trying to use a PowerBI API which saves Unity data. I have scoured the internet and I think it might be due to the PowerBI API permissions but I don’t know why Unity won’t accept my POST resquest(see below). In order for Unity to write data to the API I have been trying to use the UnityWebRequest. However, I have gotten many errors. I have been testing my API with Postman and Powershell. I am able to “POST” but I can’t use “GET”, “PUT”, or anything else and get a 404 error if I try. I am hoping this won’t be an issue because I just want to POST anyway. I am not sure if I need to change the PowerBI API permissions somehow or generate a token, I am new to PowerBI and am using it to post Unity analytics - the client insists on PowerBI otherwise I would be using Unity Cloud Save trust me.
I created a PowerBI streaming datasource API with a URL and desired data in the following format:
[ { “Variable 1” :98.6, “Variable 2” :98.6 } ]
Here are two different ways I have tried to code this:
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
public class PlayerData
{
public int Experience;
public int Confidence;
}
public void StartPost()
{
StartCoroutine(PostData());
}
IEnumerator PostData()
{
//fills player data class with users metrics
PlayerData player = new PlayerData();
player.Experience = 3;
player.Confidence = 5;
string URL = "https://blahblahblah";
string jsonData = JsonUtility.ToJson(player);
UnityWebRequest request = new UnityWebRequest.Post(URL, jsonData);
yield return request.SendWebRequest();
if (request.error != null)
{
UnityEngine.Debug.Log("Error: " + request.error);
}
else
{
UnityEngine.Debug.Log("Status Code: " + request.responseCode);
}
}
This gives me an error of: The type name ‘Post’ does not exist in the type ‘UnityWebRequest’.
The other way I do it is to change PostData to use UploadHandlerRaw:
IEnumerator PostData()
{
//fills player data class with users metrics
PlayerData player = new PlayerData();
player.Experience = 3;
player.Confidence = 5;
string jsonData = JsonUtility.ToJson(player);
byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonData);
UnityWebRequest request = new UnityWebRequest(URL, "POST");
request.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
request.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
yield return request.SendWebRequest();
if (request.error != null)
{
UnityEngine.Debug.Log("Error: " + request.error);
}
else
{
UnityEngine.Debug.Log("Status Code: " + request.responseCode);
}
}
I apologize for the lengthy post but I have been trying to figure this out for weeks now.
UPDATE: I have tried deleting the “new” from the UnityWebRequest.POST line and I now get an 500 Internal Server Error.
TLDR: I can only POST from Postman and Powershell, not any other API requests, and I can’t do anything from Unity. Any and all help is greatly appreciated.