telegraph icon indicating copy to clipboard operation
telegraph copied to clipboard

Significantly reduce size of payload

Open mercuree opened this issue 3 years ago • 0 comments

When creating telegraph page, by default requests.post is sending data with 'Content-type': 'application/x-www-form-urlencoded'

It's huge, because json payload is percent-encoded, for example this dict (43 bytes): {"key": "value with non-ascii: абвгд"} looks like this when sending request (97 bytes): %7B%22key%22%3A%20%22value%20with%20non-ascii%3A%20%5Cu0430%5Cu0431%5Cu0432%5Cu0433%5Cu0434%22%7D

Telegra.ph accepts 'Content-Type': 'application/json' header and you can use this feature instead to avoid percent-encoding.

  1. Use 'Content-Type': 'application/json' instead of default percent-encoding
  2. Use json.dumps(data, ensure_ascii=False). Telegra.ph accepts utf-8 and no need to escape into \uXXXX for non-ascii characters.
  3. Use separators without spaces json.dumps(data, ensure_ascii=False, separators=(',', ':'))
  4. Also you don't need to separately convert content property to string, it can be sent like regular json

Unfortunately, this will not help to put more than 64 kb on a page, it helps only for reducing network overhead.

mercuree avatar Nov 20 '22 12:11 mercuree