I am trying to use a PowerBI API which saves Unity data. I know there are a lot of solved questions online for Unity and APIs with Web Requests but I have scoured the internet and I think it might be due to the PowerBI API permissions. My client REALLY wants to use Azure/PowerBI for this and not Unity Cloud or Google so that is why I am using it. 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 "GET", "PUT", or anything else and get a 404 error. I am hoping this won't be an issue because I just want to post anyway. I don't seem to be able to change the API permissions through PowerBI so that is a constraint.
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);
}
}
This code gives me a 400 Bad Request message with the full stack trace:
Here is the full stacktrace as requested:
Error: HTTP/1.1 400 Bad Request
0x00007ff6f6258e4d (Unity) StackWalker::GetCurrentCallstack
0x00007ff6f625ddf9 (Unity) StackWalker::ShowCallstack
0x00007ff6f722b121 (Unity) GetStacktrace
0x00007ff6f78ed922 (Unity) DebugStringToFile
0x00007ff6f51a97f6 (Unity) DebugLogHandler_CUSTOM_Internal_Log
0x0000024745b03d93 (Mono JIT Code) (wrapper managed-to-native) UnityEngine.DebugLogHandler:Internal_Log (UnityEngine.LogType,UnityEngine.LogOption,string,UnityEngine.Object)
0x0000024745b03ccb (Mono JIT Code) UnityEngine.DebugLogHandler:LogFormat (UnityEngine.LogType,UnityEngine.Object,string,object[])
0x0000024745b03a50 (Mono JIT Code) UnityEngine.Logger:Log (UnityEngine.LogType,object)
0x0000024745b03918 (Mono JIT Code) UnityEngine.Debug:Log (object)
0x0000024745afe92b (Mono JIT Code) APIPost/<PostData>d__10:MoveNext () (at C:/Users/ellai/My project (1)/Assets/API_Post.cs:74)
0x00000246fafcccf0 (Mono JIT Code) UnityEngine.SetupCoroutine:InvokeMoveNext (System.Collections.IEnumerator,intptr)
0x00000246fafcce1f (Mono JIT Code) (wrapper runtime-invoke) <Module>:runtime_invoke_void_object_intptr (object,intptr,intptr,intptr)
0x00007ffa7ff6e0d4 (mono-2.0-bdwgc) mono_jit_runtime_invoke (at C:/build/output/Unity-Technologies/mono/mono/mini/mini-runtime.c:3445)
0x00007ffa7feaeb74 (mono-2.0-bdwgc) do_runtime_invoke (at C:/build/output/Unity-Technologies/mono/mono/metadata/object.c:3066)
0x00007ffa7feaed0c (mono-2.0-bdwgc) mono_runtime_invoke (at C:/build/output/Unity-Technologies/mono/mono/metadata/object.c:3113)
0x00007ff6f6177bc4 (Unity) scripting_method_invoke
0x00007ff6f6156844 (Unity) ScriptingInvocation::Invoke
0x00007ff6f611f32a (Unity) Coroutine::Run
0x00007ff6f611cd1f (Unity) Coroutine::ContinueCoroutine
0x00007ff6f5dd9ef3 (Unity) AsyncOperation::InvokeCoroutine
0x00007ff6f6714f8c (Unity) UnityWebRequestAsyncOperation::InvokeCoroutine
0x00007ff6f6715171 (Unity) UnityWebRequestProto<UnityWebRequestTransport,AtomicRefCounter,RedirectHelper,ResponseHelper,DownloadHandler,UploadHandler,CertificateHandler,HeaderHelper,AsyncOperation>::Job_InvokeCoroutine
0x00007ff6f5d8d63a (Unity) BackgroundJobQueue::ExecuteMainThreadJobs
0x00007ff6f5e0fb1c (Unity) `InitPlayerLoopCallbacks'::`2'::EarlyUpdateExecuteMainThreadJobsRegistrator::Forward
0x00007ff6f5defbda (Unity) ExecutePlayerLoop
0x00007ff6f5defd66 (Unity) ExecutePlayerLoop
0x00007ff6f5df6625 (Unity) PlayerLoop
0x00007ff6f6db2cbf (Unity) PlayerLoopController::InternalUpdateScene
0x00007ff6f6dbf89d (Unity) PlayerLoopController::UpdateSceneIfNeededFromMainLoop
0x00007ff6f6dbdba1 (Unity) Application::TickTimer
0x00007ff6f723180a (Unity) MainMessageLoop
0x00007ff6f7236910 (Unity) WinMain
0x00007ff6f862133e (Unity) __scrt_common_main_seh
0x00007ffaead8257d (KERNEL32) BaseThreadInitThunk
0x00007ffaed06aa78 (ntdll) RtlUserThreadStart
UPDATE: I have tried deleting the "new" from the UnityWebRequest.POST line and I now get a 500 Internal Server Error.
I have also tried writing out the string and passing it directly to Post but this still doesn't work. Any help would be greatly appreciated.