简体   繁体   English

在 Unity 中向 Notion API 发出 POST 请求

[英]making POST request to Notion API in Unity

I'm trying to make POST request in Unity to Notion API.我正在尝试在 Unity 中向 Notion API 发出 POST 请求。 I have a class with all of the properties which I created based on the Notion requirements.我有一个类,其中包含我根据 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;
        }
    }

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.这就是我调用它的方式,我尝试将 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();

and that's the error I'm getting which is not very helpful.这就是我得到的错误,这不是很有帮助。 在此处输入图片说明

Any help is appriciated.任何帮助是appriciated。

Edit: I have deleted { get;编辑:我已经删除了 { get; set;放; } like derHugo suggested however I also needed to make some of the fields with small letters eg.就像 derHugo 建议的那样,但是我还需要用小写字母制作一些字段,例如。 Database_id to database_id. Database_id 到 database_id。

The Unity Serializer doesn't support properties ! Unity Serializer 不支持属性

See ( Script Serialzation ).请参阅(脚本序列化)。

Simply remove all the {get; set; }只需删除所有{get; set; } {get; set; } {get; set; } so instead of properties you will have fields {get; set; }所以不是属性,你将有

[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)因为它是统一的,我不能使用 Newtonsoft.Json,(否则这将是非常简单的任务)

Of course you can !当然可以

It is even a package: NewtonSoft JSON you can simply install via the Package Manager它甚至是一个包: NewtonSoft JSON你可以通过包管理器简单地安装

在此处输入图片说明

afaik it even comes preinstalled in newest Unity versions afaik 它甚至预装在最新的 Unity 版本中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM