- Home /
Why is my upload/download WebRequest not working?
I'm attempting to save data to an Azure server (just storing and retrieving file info) using UnityWebRequest. I save the object CourseData and convert it to JSON, which is then converted to a Byte array to be passed into the UnityWebRequest.Put() method alond with the datapath.
The datapath is an SAS (Shared Access Signature) uri for location of the file on the server. (If I use a regular uri then I receive errors for not having correct Header formatting which really translated to not having a correct x-ms-version in the Response Header since Azure's Storage accounts apparently don't issue those Headers in the regular uri.
Here's some of those code.
public static IEnumerator UploadCourseData(CourseData data, string dataPath) {
Debug.Log("Attempting to upload data to path : " + dataPath);
UnityWebRequest www = new UnityWebRequest();
if (www.isNetworkError || www.isHttpError)
{
Debug.Log(www.error);
Debug.Log("Upload error, could not upload courses");
}
else
{
string courseData = JsonUtility.ToJson(data);
Debug.Log("This is the JSON version:" +courseData);
byte[] C_data = System.Text.Encoding.UTF8.GetBytes(courseData);
Debug.Log("This is byte data:" + C_data + "This is the data size" + C_data.Length);
www = UnityWebRequest.Put(dataPath,"/idbfs-https://smtc-cgc-azurewebsites.net");
yield return www.SendWebRequest();
Debug.Log("Upload Successful");
}
}
public static IEnumerator DownloadCourseData(string dataPath)
{
if (courseSaveFile == null)
{
courseSaveFile = new CourseData();
Debug.Log("new CD created");
}
Debug.Log("Attempting to retrieve data from path : " + dataPath);
using (UnityWebRequest web = UnityWebRequest.Get(dataPath)) {
yield return web.SendWebRequest();
if (web.isNetworkError || web.isHttpError)
{
Debug.Log(web.error);
Debug.Log("Download error, could not retrieve courses");
}
else{
//web.downloadHandler = new DownloadHandlerBuffer();
Debug.Log(web.downloadHandler.data.Length + " Current download data length");
byte[] C_data = web.downloadHandler.data;
string data = JsonUtility.ToJson(C_data);
Debug.Log("This is the JSON version:" + data);
CourseData courseData = JsonUtility.FromJson<CourseData>(data);
courseSaveFile = courseData;
Debug.Log("Download Successful, Course Data Count: " + courseSaveFile.SavedCourseData.Count);
Debug.Log("Data stats: C-data: " + courseData.SavedCourseData.Count + "string data: " + data + " -- Byte data: " + C_data);
}
}
}
Answer by misher · Oct 09, 2019 at 07:18 AM
UnityWebRequest.Put(url, data)
requires 2 arguments, the first one is the complete URL string and the second one is you byte array with data.
You also might want to set the correct header for your request>
www.SetRequestHeader("Content-Type", "application/json");
Thanks for the feedback. I changed some of the code but ran into the following error:
HTTP403: FORBIDDEN - The server understood the request, but is refusing to fulfill it. (XHR)OPTIONS - https://smtcdiag.file.core.windows.net/smtc/SavedCourseData_S$$anonymous$$TC5.dat?sv=2018-03-28&si=smtc-16D97762738&sr=f&sig=DSfgpxuaUF0hLWLVhRZjc7R7$$anonymous$$tIbvXnaD%2FngwiVi1Pg%3D
The code reads: string courseData = JsonUtility.ToJson(data); Debug.Log("This is the JSON version:" +courseData); byte[] C_data = System.Text.Encoding.UTF8.GetBytes(courseData); Debug.Log("This is byte data:" + C_data + "This is the data size" + C_data.Length); www = UnityWebRequest.Put(dataPath,C_data); www.SetRequestHeader("Content-Type", "application/json"); yield return www.SendWebRequest(); Debug.Log("Upload Successful");
The web console in Chrome shows: 403 (CORS not enabled or no matching rule found for this request.) .... as been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
I tried changing various header settings in the CORS found for the Storage Account on Azure but no luck.
Your answer
Follow this Question
Related Questions
Unable to upload Image to server with MultipartFormFileSection , WWWForm and Upload Handler 0 Answers
Failed to upload image over custom server 3 Answers
UnityWebRequest how to upload a file/gameobject 0 Answers
Download images from server and storing it in Resources. 1 Answer
Post Reqest with dictionary 1 Answer