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.
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.