Follow Pagination When making Get requests.

SOLVED
Go to solution
Occasional Participant

Follow Pagination When making Get requests.

I am trying to pull data via the api in-order to import it into a database ( FileMaker to be specific ).  I am having a probem following pagination and the next links in-order to get all of our data.  I am trying to use Python and the requests module to get this data, but when I try to get the value of ['next_link'] in python I keep getting a KeyError.  I am not married to Python, in fact I know very very little, however, it works with filemaker, and so far I can get the first page of any query, I am just not able to follow the next links.  Does anyone, or is anyone willing to share how they follow pagination in a script?  

 

 

Thanks!!! 

1 ACCEPTED SOLUTION
Member

Hello,

 

It's possible that you might not be going far enough into the object structure to get the next link. Here's a quick example of how you could get the link:

if "next_link" in response["meta"]["pagination"]:
	next = response["meta"]["pagination"]["next_link"]

In the above code I also show the best way to determine if a next link is present or not, so you know if you need to continue with pagination. You can read more about how pagination works with our API here: http://developer.constantcontact.com/docs/developer-guides/paginated-output.html

 

Please let me know if you have any questions!

 

Sincerely,

Elijah G.
API Support Engineer

View solution in original post

3 REPLIES 3
Occasional Participant

Never mind I must have an error in my original code, it now works.  For reference if you use python and requests you get the next link like this:

 

import requests

uri = 'https://api.constanctcontact.com'
params = dict (
    status = '<status>',
    limit = '<limit>',
    api_key = '<apikey>'
)
headers = {'Authorization': 'Bearer <your_auth_key>'}
current = '<api url>' // Example for contacts it would be  '/v2/contacts'
r = requests.get(uri + current , params = params , headers = headers )
raw = r.json()  
// below will print the next link only but you can set as a variable and loop to grab all contacts  
print raw ['meta'] ['pagination'] ['next_link']

Member

Hello,

 

It's possible that you might not be going far enough into the object structure to get the next link. Here's a quick example of how you could get the link:

if "next_link" in response["meta"]["pagination"]:
	next = response["meta"]["pagination"]["next_link"]

In the above code I also show the best way to determine if a next link is present or not, so you know if you need to continue with pagination. You can read more about how pagination works with our API here: http://developer.constantcontact.com/docs/developer-guides/paginated-output.html

 

Please let me know if you have any questions!

 

Sincerely,

Elijah G.
API Support Engineer

View solution in original post

Occasional Participant

Yes and thank you!!!!! Will need to add the if statment to my loop, that is great thank you so much!!  

Developer Portal

View API documentation, code samples, get your API key.

Visit Page