I’m having trouble with the Notion API in Unity. My code works fine for the initial query of my database, but when I use the start_cursor
to fetch additional data, I consistently receive a 400 Bad Request error.
Here’s my approach:
- I make a normal query to the database.
- I capture the
next_cursor
from the response.
- I use this cursor in my subsequent request.
Despite following this process, the subsequent queries are unsuccessful. Could someone point out if there’s a specific method required to use the cursor effectively?
Below is a revised version of my code:
string nextCursor = null;
bool hasMore = true;
while (hasMore) {
string queryUrl = nextCursor == null ? baseUrl : baseUrl + $"?start_cursor={nextCursor}";
var response = await FetchData(queryUrl);
if (response.HasMore) {
nextCursor = response.NextCursor;
} else {
hasMore = false;
}
}
Any insights on why this might be happening? Thanks!
I’ve encountered a similar issue when working with the Notion API in Unity. The problem might be in how you’re formatting the URL for subsequent requests. Instead of appending the cursor to the URL, try including it in the request body.
Here’s what worked for me:
- For the initial request, use your current approach.
- For subsequent requests, keep the same URL but modify the request body:
var requestBody = new {
start_cursor = nextCursor
};
var jsonBody = JsonConvert.SerializeObject(requestBody);
// Use this jsonBody in your HTTP request
This method ensures the cursor is properly included in the request. Also, double-check that your authorization headers are correctly set for each request. Let me know if this helps resolve your 400 error!
I’ve dealt with this exact problem in my own Notion API integrations. The issue often lies in how the API handles pagination parameters. In my experience, the solution was to modify how I structured the request payload.
Instead of appending the cursor to the URL, I found success by including it in the JSON body of the POST request. Here’s a snippet that worked for me:
var payload = new Dictionary<string, object>
{
{ "start_cursor", nextCursor },
{ "page_size", 100 } // Adjust as needed
};
string jsonPayload = JsonConvert.SerializeObject(payload);
// Use jsonPayload in your POST request body
Also, ensure you’re using the correct endpoint for querying databases (/v1/databases/{database_id}/query). This approach resolved my 400 errors and allowed for smooth pagination through large datasets.
Remember to handle rate limits too - I usually implement a small delay between requests to avoid hitting Notion’s API limits. Hope this helps you resolve the issue!
hey man, i had similar probs with notion api. try sending the cursor in the request body instead of url. something like this:
var body = new { start_cursor = nextCursor };
var json = JsonUtility.ToJson(body);
then use this json in ur POST request. also check ur auth headers r correct for each request. hope this helps!