1

I'm trying to make POST request in Unity to Notion API. I have a class with all of the properties which I created based on the Notion requirements.

    [Serializable]
    public class Parent
    {
        public string Database_id { get; set; }
        public Parent(string database_id)
        {
            Database_id = database_id;
        }
    }

    [Serializable]
    public class Text
    {
        public string Content { get; set; }

        public Text(string content)
        {
            Content = content;
        }
        //public List<RichText> rich_text { get; set; }
    }

    [Serializable]
    public class Title
    {
        public Text Text { get; set; }
        public Title(Text text)
        {
            Text = text;
        }
    }

    [Serializable]
    public class Name
    {
        public List<Title> title { get; set; }
        public Name(List<Title> titles)
        {
            title = titles;
        }
    }

    [Serializable]
    public class Properties
    {
        public Name Name { get; set; }

        public Properties(Name name)
        {
            Name = name;
        }
    }

    [Serializable]
    public class Root
    {
        public Parent Parent { get; set; }
        public Properties Properties { get; set; }

        public Root(Parent parent, Properties properties)
        {
            parent = parent;
            properties = properties;
        }
    }

And this is the way I'm calling it, I tried converting json string to bytes but I was getting error that it's wrong json format and the way I have right now makes some progress but says parent is undefined when it is.

var url = $"https://api.notion.com/v1/pages";
        var parent = new Parent(databaseId);
        var txt = new Text("test");
        var title = new Title(txt);
        var nam = new Name(new List<Title>() { title });
        var prop = new Properties(nam);
        var root = new Root(parent, prop);


        string json = JsonUtility.ToJson(root);

        UnityWebRequest www = new UnityWebRequest(url, "POST");
        byte[] bodyRaw = Encoding.UTF8.GetBytes(json);
        www.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
        www.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();

        www.SetRequestHeader("Authorization", userSecret);
        www.SetRequestHeader("notion_version", Static.NOTION_VER);
        www.SetRequestHeader("Content-Type", "application/json");

        yield return www.SendWebRequest();

and that's the error I'm getting which is not very helpful. enter image description here

Any help is appriciated.

Edit: I have deleted { get; set; } like derHugo suggested however I also needed to make some of the fields with small letters eg. Database_id to database_id.

  • Have you tried looking at the shape of the `root` object, maybe there is something wrong in the way it's constructured? Probably easier to just manipulate the JSON directly. – picsoung Jun 22 '21 at 05:05
  • because it's unity I can't use Newtonsoft.Json, (otherwise it would be very simple task) so I tried to create my json like this inside the string string data = "{\"parent\": {\"database_id\": \"833d2ab361a74885a639749df796f84e\"},\"properties\": {\"Name\": {\"title\": [{\"text\": {\"content\": \"test1\"}}]}}"; but it didn't work at all and said invalid json – Michael Krezalek Jun 22 '21 at 06:24

1 Answers1

2

The Unity Serializer doesn't support properties!

See (Script Serialzation).

Simply remove all the {get; set; } so instead of properties you will have fields

[Serializable]
public class Parent
{
    public string Database_id;

    public Parent(string database_id)
    {
        Database_id = database_id;
    }
}

[Serializable]
public class Text
{
    public string Content;

    public Text(string content)
    {
        Content = content;
    }
}

[Serializable]
public class Title
{
    public Text Text;

    public Title(Text text)
    {
        Text = text;
    }
}

[Serializable]
public class Name
{
    public List<Title> title;

    public Name(List<Title> titles)
    {
        title = titles;
    }
}

[Serializable]
public class Properties
{
    public Name Name;

    public Properties(Name name)
    {
        Name = name;
    }
}

[Serializable]
public class Root
{
    public Parent Parent;
    public Properties Properties;

    public Root(Parent parent, Properties properties)
    {
        parent = parent;
        properties = properties;
    }
}

because it's unity I can't use Newtonsoft.Json, (otherwise it would be very simple task)

Of course you can!

It is even a package: NewtonSoft JSON you can simply install via the Package Manager

enter image description here

afaik it even comes preinstalled in newest Unity versions

derHugo
  • 83,094
  • 9
  • 75
  • 115
  • it works, thank you, completely forgot about that, however I had to make one more change to that which was to match the case of the words with what is expected eg. parent and not Parent. – Michael Krezalek Jun 22 '21 at 18:57
  • @MichaelKrezalek well yes of course ;) you didn't post your JSON though, so we had to assume that is already the case ;) – derHugo Jun 22 '21 at 19:26