0

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
      }
    ]
  }
}

2 Answers2

1

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.

Ryan Man
  • 26
  • 1
0

You also need to change data=new_body to json=new_body

  • Thanks for contributing to StackOverflow! Considering that your answer is an addition to an earlier (already accepted) answer, I suggest that you add your answer as a comment to the earlier answer. – Hennep Aug 15 '23 at 22:28
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/34836911) – geanakuch Aug 17 '23 at 12:28