#UnityWebRequest to class

1 messages · Page 1 of 1 (latest)

cloud breach
#

This is my class

   public class User
    {
        public string name;
        public string email;
        public string signedupDate;
        public int numberOfOrganizations;
        public Organization[] organizations;
    }

    public class Organization
    {
        public int id;
        public string name;
        public OrganizationGame[] games;
    }

    public class OrganizationGame
    {
        public int id;
        public bool isDemo;
        public string name;
        public string badgeURL;
        public string logoURL;
    }


    public class UserRequest
    {
        public string email;
        public string password;
    }

    public class UserResponse
    {
        public string token;
        public User data;
    }
#

        var user = new AdminResponse.UserRequest();
        user.email = email;
        user.password = password;

        string json = JsonUtility.ToJson(user);

        UnityWebRequest www = new UnityWebRequest(baseURL + sessionURL, "POST", new DownloadHandlerBuffer(), new UploadHandlerRaw(Encoding.UTF8.GetBytes(json)));
 
        www.SetRequestHeader("Content-Type", "application/json");
        www.SetRequestHeader("LL-Version", "2021-03-01");
        yield return www.SendWebRequest();

        if (www.result != UnityWebRequest.Result.Success)
        {
            Debug.Log(www.error);
            Debug.Log(json);

        }
        else
        {
            Debug.Log("Admin successfully logged in!");
            LoginPanel.SetActive(false);

            var result = JsonUtility.ToJson(www.downloadHandler.text);
            
            
        }
placid marlin
#

What's the response look like

toxic vault
#

ew

#

mikkels no

#

stop

#

do not ever use JsonUtility

cloud breach
#

This is result

toxic vault
#

but also you meant FromJson not ToJson

#

but also just don't use JsonUtility

cloud breach
toxic vault
#

that says ToJson buddy

cloud breach
#

Es

#

Yes

#

I want result to json

#

.text is a string

toxic vault
#

the json string, right?

cloud breach
#

Probably now i'm confused

placid marlin
#

No you want it FromJSON kekw

toxic vault
#

^

placid marlin
#

string From JSON Object

cloud breach
#

But the request returns a string

toxic vault
#

a json string yes

cloud breach
#

That I want to make into Json

#

Oh

placid marlin
toxic vault
#

smh

#

but seriously don't use JsonUtility

placid marlin
#

Putting that aside GSON or mapping would be better

cloud breach
#

OH

#

admin = JsonUtility.FromJson<AdminResponse.UserResponse>(www.downloadHandler.text);

placid marlin
#

Unless Yasa has some fancy magic I'm unaware of

cloud breach
toxic vault
#

because it's shit

#

it only supports types that the unity serialization system supports. which also means no dictionaries

#

one of the most popular fucking types to serialize

#

it's awful

cloud breach
#

Oh yeah I know

#

I dont need dictionaries tho

#

all good

toxic vault
#

it also does not support property naming

cloud breach
#

Also

#

Wanna know something funny

toxic vault
#

or custom converters

#

or literally anything

placid marlin
#

Honestly I wouldn't even use UnityWebRequest, what does it provide over RestSharp

cloud breach
#

UnityWebRequest

#

Is shiiite

toxic vault
#

don't use it then

cloud breach
#

if I want to Post a json type

#

I can't

#

Because it forces the content type to be form

placid marlin
#

RestSharp in an async method

cloud breach
toxic vault
#

why restsharp

#

just use the built in HttpClient

placid marlin
#

Beautiful mapping and body construction

#

They recently updated, it's not as much of a nightmare now

#

But built in works really good too

cloud breach
#

hmm

#

It no work wah

#

Ah it work

analog tiger
# cloud breach I can't

Does the following not post a json?

            HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(MyURL);
            httpWebRequest.ContentType = "application/json";
            httpWebRequest.Method = "POST";

            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {
                MyClass obj = ...
                ...

                string json = JsonConvert.SerializeObject(obj);
                streamWriter.Write(json);
            }

I actually don't know. I got it from somewhere else. var

cloud breach
#

But yeah it works with that

toxic vault