0

I've been trying to query the Notion API using Python, and as I have more than 100 results, I need to either filter them down or be able to paginate. I tried different variants of the following code to no avail:

headersNotion = {
"Authorization": "Bearer " + notionToken,
"Content-Type": "application/json",
"Notion-Version": "2021-05-13" 
}

data = { 
    "start_cursor" : nextCursor
}

readUrl = f"https://api.notion.com/v1/databases/{databaseId}/query" 

res = requests.request("POST", readUrl, data=data, headers=headers)

I've also tried with data = { "filter": {"cover" : "is_empty"} } or even with an idiotic or empty filter, but as soon as I add any sort of data to the request, I get a 400 error:

{"object": "error", "status": 400, "code": "invalid_json", "message": "Error parsing JSON body."}

Would anyone have any idea what I might be doing wrong?

Ali
  • 3
  • 1
  • 7

1 Answers1

0

look here headersNotion schould be headers. https://developers.notion.com/reference/post-database-query

Just as example.

payload = {
    'filter': {
        'and': [
        {
                'property': 'aktualisiert am',
                'date': {
                    'after': str(age)
                }
            },
            {
                'property': 'aktiv',
                'text': {
                    'equals': 'yes'
                }
            }
        ]
    }
}

headers = {
"Accept": "application/json",
"Authorization": "Bearer " + token,
"Content-Type": "application/json",
"Notion-Version": "2021-08-16"
}

def readDatabase(databaseId, headers, payload):
readUrl = f"https://api.notion.com/v1/databases/{databaseId}/query"
res = requests.request("POST", readUrl, json=payload, headers=headers)
datadb = res.json()
print(res.text) #for Debug

# Errors Returns a 404 HTTP response if the database doesn't exist, or if the integration doesn't have access to the database.
#        Returns a 400 or a 429 HTTP response if the request exceeds the request limits.
#        Returns 200 HTTP responce if OK
#print ('')
print ('')
if res.status_code == 200:
    print('200 Success!') # Yes everything OK!
elif res.status_code == 404:
    print('404 Not Found.')
    #sendmail
    subject = " Fehler!! 404 Database Not Found"
    send_email(user, password, recipient, subject, body)
    return
elif res.status_code == 429:
    print(' Fehler !! 429 request exceeds the request limits.')    
    #sendmail
    subject = " Fehler !!  429 request exceeds the request limits."
    send_email(user, password, recipient, subject, body)
    return
elif res.status_code == 400:
    print('400 request exceeds the request limits.')      
    #sendmail
    subject = " Fehler !!  400 request exceeds the request limits. "
    send_email(user, password, recipient, subject, body)
    return
# print(res.text)
# write csv File 
with open('./dbmain.json', 'w', encoding='utf8') as f:
    json.dump(datadb, f, ensure_ascii=False)
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/31095605) – MD Mushfirat Mohaimin Feb 22 '22 at 00:32