HTTP 400 Bad Request when using Notion API database query with start_cursor Parameter in Unity

So I’m trying to request a query from my notion database, but since the default maximum of page_size pagination is 100, I decided to use the next_cursor to get all the data in my database. Logically, I temporarily save all of the response in a list until there’s no more pages left.

The code works very well if I just request a default database using https://api.notion.com/v1/databases/{databaseID}/query. The eror happens when I tried to get the next page after using the previous next_cursor. The error is HTTP/1.1 400 Bad Request.

Could someone help me out why this is happen, I’m pretty sure its the right API?

This is my code:

 public class NotionDatabase<T> : NotionObject where T : INotionProperty, new()
 {
     public NotionDatabase(string apiKey, string databaseID) : base(apiKey)
     {
         ID = databaseID;
         EndPointQuery = $"https://api.notion.com/v1/databases/{databaseID}/query";
     }

     protected readonly string ID;
     protected readonly string EndPointQuery;
     protected Database<T> Result;

     public virtual async void Get(Action<T[]> onSuccess = null, Action<string> onError = null)
     {
         List <DatabaseEntry<T>> allResult = new(); // Store all result from request

         string nextCursor = null;
         bool hasMore = true;

         RateLimiter rateLimiter = new RateLimiter(2);

         while (hasMore)
         {
             await rateLimiter.WaitAsync(); // Wait for rate limiting

             string queryEndpoint = string.IsNullOrEmpty(nextCursor) ? EndPointQuery : EndPointQuery + $"?start_cursor={nextCursor}";
             Debug.Log(queryEndpoint);

             using var req = WebRequestWithAuth(queryEndpoint, RequestType.POST);
             req.SendWebRequest();
             while (!req.isDone)
             {
                 await Task.Yield();
             }
             if (req.result != UnityWebRequest.Result.Success)
             {
                 Debug.Log($"Get Json {ID} failed ({req.error}).");
                 onError?.Invoke(req.error);
                 return;
             }
             Result = JsonUtility.FromJson<Database<T>>(req.downloadHandler.text);
             allResult.AddRange(Result.results);

             if (GetHasMoreValue(req.downloadHandler.text) == "false")
             {
                 hasMore = false;
             }
             else
             {
                 nextCursor = GetNextCursorValue(req.downloadHandler.text);
             }

         }
         onSuccess?.Invoke(allResult.ConvertAll(x => x.properties).ToArray());
     }```

  • What is webrequestwithauth? Its telling you what you sent was not what it wanted so you need that

    – 




  • Its a unitywebrequest, basically its just a shortcut method for me to write headers and my API request

    – 

  • The error only happens when I add the start_cursor parameter in the URL. I guess thats not how you use it though (ps: I already test it too in API request online website and its the same error 400. Something about the syntax of the url

    – 

Leave a Comment