'Python - pagination "after" parameter doesnt work in query

Im making a code to get data from pipefy API and the pagination isn't working out for me

headers = {
    "authorization": 'Bearer TOKEN',
    "content-type": "application/json"
}
first_query = True
has_next_page = True
pipe_id = "PIPE_ID_NUMBER"
    if first_query:
        payload = "{\"query\":\"{ allCards (pipeId:"+pipe_id+", first:50){edges{node{id title fields {report_value updated_at}} cursor} pageInfo {endCursor hasNextPage}}}\"}"
        print(payload)
        first_query = False
    else:
        payload = "{\"query\":\"{allCards(pipeId:"+pipe_id+",after:\""+end_cursor+"\"){edges{node{id title fields{report_value updated_at}}}pageInfo{endCursor hasNextPage}}}\"}"
        print(payload)
    response = requests.request("POST", url, data=payload, headers=headers)
    print(response)    
    json_data = json.loads(response.text)
    end_cursor = json_data["data"]["allCards"]["pageInfo"]["endCursor"]
    has_next_page = json_data["data"]["allCards"]["pageInfo"]["hasNextPage"]
    total_records_pg = len(json_data["data"]["allCards"]["edges"])

and i got the return:

{"query":"{ allCards (pipeId:PIPE_ID, first:50){edges{node{id title fields {report_value updated_at}} cursor} pageInfo {endCursor hasNextPage}}}"}
<Response [200]>
{"query":"{allCards(pipeId:PIPE_ID,after:"WyIyLjAiLCItNTcuMCIsNTA0NDMxMzg4XQ"){edges{node{id title fields{report_value updated_at}}}pageInfo{endCursor hasNextPage}}}"}
<Response [400]>
Traceback (most recent call last):
  File "c:\Users\rafael.alves\Desktop\Novo PC\uknow\DigitalPE\import requests.py", line 32, in <module>
    json_data = json.loads(response.text)
  File "C:\Users\rafael.alves\AppData\Local\Programs\Python\Python310\lib\json\__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "C:\Users\rafael.alves\AppData\Local\Programs\Python\Python310\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\rafael.alves\AppData\Local\Programs\Python\Python310\lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
PS C:\Users\rafael.alves\Desktop\Novo PC\uknow> 

have looking everywhere where i got it wrong but no with no succes. the "<Response [400]>" tells me that the payload just bring empty, and i supossed that means the mistake is the "after" argument



Solution 1:[1]

I made it work

 if first_query:
        payload = "{\"query\":\"{ allCards (pipeId:%d, first:50){edges{node{id title phases_history {phase{name}}fields {report_value   name updated_at}} cursor} pageInfo {endCursor hasNextPage}}}\"}" % (
            pipe_id)
        first_query = False
    else:
        payload = '{"query": "{allCards(pipeId: %d,after: \\"%s\\"){edges{node{id title phases_history {phase{name}} fields{report_value name updated_at}}}pageInfo{endCursor hasNextPage}}}"}' % (
            pipe_id, end_cursor)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rafael Angelos