Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How should paging be used in rest APIs #111

Closed
dancb10 opened this issue Feb 13, 2023 · 6 comments
Closed

How should paging be used in rest APIs #111

dancb10 opened this issue Feb 13, 2023 · 6 comments

Comments

@dancb10
Copy link

dancb10 commented Feb 13, 2023

While there are some examples here and here on how paging should work in theory, these examples do not clearly show on how you would use the paging state in a Json rest API.
So the theory is that you execute a select statement, save the current paging state, return it to the user in string format and then use it the next time the user requests the next page.

I've tried using the examples you've posted such as:
I have a struct that contains the paging state parameter

type MessagesRestDetails struct {
  PageState string `form:"pagestate" binding:"omitempty"`
}

And when a request is sent I bind it to a struct variable

var messagesRestDetails structs.MessagesRestDetails        
if err := c.BindQuery(&messagesRestDetails); err != nil {
  c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  return
}

I then execute the query as

var pageState []byte
if messagesRestDetails.PageState != "" {
  ps, err := base64.StdEncoding.DecodeString(messagesRestDetails.PageState)
  if err != nil {
      c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
      return
  }
  pageState = ps
}

iter := session.Query("SELECT * FROM messages.messages WHERE gid = ?", messagesGid.GID).PageSize(3).PageState(pageState).Iter()
nextPageState := iter.PageState()

And then the nextPageState I encode it to string and return it to the client

encodedStr := base64.StdEncoding.EncodeToString([]byte(nextPageState))

So while the first time I request this it works and the first three elements are returned in the API, when using the received page state value and send it in a new request I get"error": "illegal base64 data at input byte 165". That's because the encoded string contains a '\' character.
So the question is how to properly send and receive the paging state in these requests?

@dancb10
Copy link
Author

dancb10 commented Feb 15, 2023

Hello, do you have any suggestions on this?

@mykaul
Copy link

mykaul commented Feb 15, 2023

@avelanarius - can you see if we have an idea for the above?

@dancb10
Copy link
Author

dancb10 commented Feb 15, 2023

Seems like it's a similar issues as this one but I've encoded the token and when trying to decode it I get the above mentioned error

@dancb10
Copy link
Author

dancb10 commented Feb 15, 2023

And note that because this is sent in a GET request as parameter, the path will look something like:
/messages/437407911753331250?cid=437407628419707442&pagestate=AAAAAKAAAAAUAAAAAQAAAAgAAAAGEfwtogCyMgEUAAAAAQAAAAgAAAAGM/dzTQACBPz///9/Q/VGQ2HTULAGFFSvSjmaAQAAADkAAAABGQAAABQAAAABAAAACAAAAN8XVqPvcUfIAQEZAAAAFAAAAAEAAAAIAAAA3xdWo+9xR8gBAQEAAAB8Tb0sWJ38vDUywrbsuBSiAQAAAAAA/////wAAAAA="

So I'm curious how can we pass the PageState from one request to another because even if you base64 encode it, it still doesn't work

@dancb10
Copy link
Author

dancb10 commented Feb 15, 2023

I made it work by using hex encode/decode .e.g
encodedStr := hex.EncodeToString(nextPageState)

@mmatczuk
Copy link

The StdEncoding will not work, the URLEncoding will work @dancb10.

@sylwiaszunejko sylwiaszunejko closed this as not planned Won't fix, can't repro, duplicate, stale Jun 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants