我正在尝试在 Unity 中向 Notion API 发出 POST 请求。我有一个类,其中包含我根据 Notion 要求创建的所有属性。
[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;
}
}
这就是我调用它的方式,我尝试将 json 字符串转换为字节,但我得到错误,它是错误的 json 格式,我现在的方式取得了一些进展,但说 parent 是未定义的。
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();
任何帮助都会得到帮助。
编辑:我已删除 { get; 放; 像 derHugo 建议的那样,但是我还需要用小写字母制作一些字段,例如。Database_id 到 database_id。