1

I'm implementing a REST API which behind the scenes queries Cassandra (via Python driver) and returns the result. Now the items to be queried will be huge so I want to have pagination capability.

The ResultSet returned by execute() method has a property called paging_state which seems to be byte literal. I would like to return this in the response body with something like startKey=<PAGING_STATE_VALUE>. Currently, I see the paging state looking something like b'\x0cFY5D70822742\x00\xf0\x7f\xff\xff\xe6\x00'.

How should I decode it so that I can append it as a proper string in my API response body. I tried decoding with UTF-8, ASCII but they are failing.

Sivaprasanna Sethuraman
  • 4,014
  • 5
  • 31
  • 60

1 Answers1

1

The paging state by definition is binary, and won't be represented as a string.

Just encode it as base64, quoted printable, uuencode, or hex string - for example by using built-in binascii module. There are plenty of functions there, like, hexlify to convert from bytes to hex string.

Alex Ott
  • 80,552
  • 8
  • 87
  • 132
  • Wow. That helped. I used `binascii.hexlify(paging_state).decode()` to give a hex string in response and when the API request has some value for `startKey`, I convert it to binary data by `binascii.unhexlify(startKey)` – Sivaprasanna Sethuraman Apr 25 '21 at 16:08