pythonpython-requestsnotion-api

I can't update blocks in Notion API


How can I update my block with Notion? I've tried to follow the instructions on how to update the API but failed. Even when using requests.patch, the response text doesn't change.

Here's the code:

import json, requests
import dotenv, os

def main():
    dotenv.load_dotenv(dotenv.find_dotenv())
    TOKEN = os.environ.get("TOKEN")
    BLOCK_ID = os.environ.get("BLOCK_ID")
    headers = {
        "Authorization": "Bearer " + TOKEN,
        "Notion-Version": "2021-08-16"
    }

    new_body = {
        "paragraph": {
            "text": [{
                "text": {
                    "content": "bye bye"
                }
            }]
        }
    }

    readurl = f"https://api.notion.com/v1/blocks/{BLOCK_ID}"

    # res = requests.request("PATCH", readurl, headers=headers, data=json.dumps(new_body))
    res = requests.patch(readurl, headers=headers, data=new_body)
    print(res.status_code)
    print(json.dumps(json.loads(res.text), indent=2))

if __name__ == "__main__":
    main()

The status code and text are:

200
{
  # other properties... then,
  "paragraph": {
    "text": [
      {
        "type": "text",
        "text": {
          "content": "hello there!",
          "link": null
        },
        "annotations": {
          "bold": true,
          "italic": false,
          "strikethrough": false,
          "underline": false,
          "code": false,
          "color": "default"
        },
        "plain_text": "hello there!",
        "href": null
      }
    ]
  }
}

Solution

  • cause u miss Content_Type on header

    try change to

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

    the data value will ignore when no content-Type on header, so the 200 respond will show as no data value provide for change.