Home > Enterprise >  How do I send a two string Dictionary to a server?
How do I send a two string Dictionary to a server?

Time:02-25

I'm new to Unity (and c#, along with PHP) and have been tasked with getting some old c# and PHP code working. The code below is supposed to send the dictionary (formData) to the PHP server that then converts it to json. The code is below:

...
//This code runs for each file that is uploaded, the file is a list of strings and integers.
Dictionary<string, string> formData = new Dictionary<string, string>();
using (StreamReader sr = file.OpenText())
            {
                string s = "";
                while ((s = sr.ReadLine()) != null)
                {
                    string[] data = s.Split(';');
                    uploadResultText.setText(file.Name   ":"   data[0]   " "   data[0]);
                    if (data[1] == "") data[1] = " ";
                    formData[data[0]] = data[1];
                }
            }
UnityWebRequest uploadRequest = UnityWebRequest.Post(serverBaseURL, formData);
currentUploadRequest = uploadRequest;
yield return uploadRequest.SendWebRequest();
...

If this code is working, how will I need to receive it server-side?

CodePudding user response:

After you Yield Return you can access the result:

yield return www.SendWebRequest();

if (www.result != UnityWebRequest.Result.Success)
{
    // Error
}
else
{
    var result = UnityWebRequest.Result;
}
  • Related