<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: am unable to filter response.status_code == 400 for campaigns data in APIs &amp; Integrations</title>
    <link>https://community.hubspot.com/t5/APIs-Integrations/am-unable-to-filter-response-status-code-400-for-campaigns-data/m-p/852247#M67604</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/20405"&gt;@himanshurauthan&lt;/a&gt;&amp;nbsp;sir , are you there ? can you please help me i got stuck I tried&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 20 Sep 2023 10:38:52 GMT</pubDate>
    <dc:creator>9kumar</dc:creator>
    <dc:date>2023-09-20T10:38:52Z</dc:date>
    <item>
      <title>am unable to filter response.status_code == 400 for campaigns data</title>
      <link>https://community.hubspot.com/t5/APIs-Integrations/am-unable-to-filter-response-status-code-400-for-campaigns-data/m-p/852159#M67591</link>
      <description>&lt;P&gt;am unable to put&amp;nbsp;&amp;nbsp;response.status_code == 400 if offset value is not present [error : ]and continue with the offset values which has data for the campaigns below is the code am using .. need help&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;has_more = True&lt;BR /&gt;offset = ""&lt;BR /&gt;offset_str= str(offset)&lt;BR /&gt;limit = 10&lt;/P&gt;&lt;P&gt;# List to store the data&lt;BR /&gt;offset_list=[]&lt;BR /&gt;data_list = []&lt;BR /&gt;campaign_ids_list=[]&lt;BR /&gt;prev_campaign_ids=set()&lt;BR /&gt;loopiteration=1&lt;BR /&gt;while has_more:&lt;BR /&gt;url = "&lt;A href="https://api.hubapi.com/email/public/v1/campaigns?limit=10&amp;amp;offset=" target="_blank" rel="noopener"&gt;https://api.hubapi.com/email/public/v1/campaigns?limit=10&amp;amp;offset=&lt;/A&gt;"+offset&lt;BR /&gt;print("loopiteration :",{loopiteration})&lt;BR /&gt;loopiteration +=1&lt;BR /&gt;print("samsssss",url)&lt;BR /&gt;#print(url+offset)&lt;BR /&gt;response = requests.request("GET", url, headers=headers)&lt;BR /&gt;if response.status_code ==200:&lt;BR /&gt;#json_data= response.json()&lt;BR /&gt;json_data= json.loads(response.text)&lt;BR /&gt;print(json_data)&lt;BR /&gt;campaigns =json_data.get('campaigns',[])&lt;BR /&gt;campaign_ids=set([campaign.get('id') for campaign in campaigns])&lt;BR /&gt;campaign_ids_list.extend(list(campaign_ids))&lt;BR /&gt;#campaign_ids_list.extend(campaign_ids)&lt;BR /&gt;campaign_count= len(campaign_ids)&lt;BR /&gt;print("campaign_ids:",campaign_ids)&lt;BR /&gt;print("length of prev_campaign_ids:",len(prev_campaign_ids))&lt;BR /&gt;#offset +=limit&lt;BR /&gt;# Check if there are more records&lt;BR /&gt;has_more = json_data.get("hasMore", False)&lt;BR /&gt;print("has_more",has_more)&lt;BR /&gt;if has_more:&lt;BR /&gt;offset = json_data.get("offset", str(''))&lt;BR /&gt;offset_list.append(offset)&lt;BR /&gt;print("offf",offset_list)&lt;BR /&gt;else:&lt;BR /&gt;#offset =&lt;BR /&gt;has_more=False&lt;BR /&gt;&lt;BR /&gt;else:&lt;BR /&gt;print(f"Request failed with status code {response.status_code}")&lt;BR /&gt;has_more=False&lt;/P&gt;</description>
      <pubDate>Wed, 20 Sep 2023 07:28:00 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/APIs-Integrations/am-unable-to-filter-response-status-code-400-for-campaigns-data/m-p/852159#M67591</guid>
      <dc:creator>9kumar</dc:creator>
      <dc:date>2023-09-20T07:28:00Z</dc:date>
    </item>
    <item>
      <title>Re: am unable to filter response.status_code == 400 for campaigns data</title>
      <link>https://community.hubspot.com/t5/APIs-Integrations/am-unable-to-filter-response-status-code-400-for-campaigns-data/m-p/852181#M67596</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/634425"&gt;@9kumar&lt;/a&gt;&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;The issue in your code is that you are not checking for the presence of the offset key in the JSON response before adding it to the offset_list. This can cause an error if the response does not contain the offset key, such as when the offset value is not present.&lt;BR /&gt;&lt;BR /&gt;You can use this code&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;LI-CODE lang="python"&gt;offset_list = []
has_more = True
offset = ""
offset_str= str(offset)
limit = 10

# List to store the data
data_list = []
campaign_ids_list=[]
prev_campaign_ids=set()
loopiteration=1
while has_more:
    url = "https://api.hubapi.com/email/public/v1/campaigns?limit=10&amp;amp;offset="+offset
    print("loopiteration :",{loopiteration})
    loopiteration +=1
    print("samsssss",url)
    #print(url+offset)
    response = requests.request("GET", url, headers=headers)
    if response.status_code ==200:
        #json_data= response.json()
        json_data= json.loads(response.text)
        print(json_data)
        campaigns =json_data.get('campaigns',[])
        campaign_ids=set([campaign.get('id') for campaign in campaigns])
        campaign_ids_list.extend(list(campaign_ids))
        #campaign_ids_list.extend(campaign_ids)
        campaign_count= len(campaign_ids)
        print("campaign_ids:",campaign_ids)
        print("length of prev_campaign_ids:",len(prev_campaign_ids))

        # Check if the `offset` key is present in the JSON response
        if "offset" in json_data:
            offset = json_data.get("offset", str(''))
            offset_list.append(offset)
        else:
            # The `offset` key is not present in the JSON response, so stop iterating
            has_more = False
        print("offf",offset_list)

    else:
        print(f"Request failed with status code {response.status_code}")
        has_more=False&lt;/LI-CODE&gt;
&lt;P&gt;&lt;BR /&gt;by this code you can check for the presence of the offset key in the JSON response before adding it to the offset_list. If the offset key is not present, the code will stop iterating.&lt;/P&gt;</description>
      <pubDate>Wed, 20 Sep 2023 08:21:37 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/APIs-Integrations/am-unable-to-filter-response-status-code-400-for-campaigns-data/m-p/852181#M67596</guid>
      <dc:creator>himanshurauthan</dc:creator>
      <dc:date>2023-09-20T08:21:37Z</dc:date>
    </item>
    <item>
      <title>Re: am unable to filter response.status_code == 400 for campaigns data</title>
      <link>https://community.hubspot.com/t5/APIs-Integrations/am-unable-to-filter-response-status-code-400-for-campaigns-data/m-p/852189#M67597</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/20405"&gt;@himanshurauthan&lt;/a&gt;&amp;nbsp;for swift reply&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;actually am able to get 4 requests in a loop but last request am getting error&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;Request failed with status code 400&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;so i would like to skip the response request which is failing or response.status==400 and do iteration for response==200 but loop is ending here itself because i have given has_more=True . and offset is in this format&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;A href="https://api.hubapi.com/email/public/v1/campaigns?limit=10&amp;amp;offset=AQAAAYqw+Xr4AAAAABAUh7Q=" target="_blank" rel="noopener"&gt;AQAAAYqw+Xr4AAAAAEEUh7Q=&lt;/A&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;need help here how to ignore failed response ==400 and get all response=200&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 20 Sep 2023 08:29:41 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/APIs-Integrations/am-unable-to-filter-response-status-code-400-for-campaigns-data/m-p/852189#M67597</guid>
      <dc:creator>9kumar</dc:creator>
      <dc:date>2023-09-20T08:29:41Z</dc:date>
    </item>
    <item>
      <title>Re: am unable to filter response.status_code == 400 for campaigns data</title>
      <link>https://community.hubspot.com/t5/APIs-Integrations/am-unable-to-filter-response-status-code-400-for-campaigns-data/m-p/852192#M67598</link>
      <description>&lt;P&gt;ok this is the modified code&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;LI-CODE lang="python"&gt;import requests
import json

# Initialize variables
offset = ""
limit = 10
data_list = []
campaign_ids_list = set()  # Use a set to ensure unique campaign IDs

while True:  # Infinite loop to handle skipping requests with status code 400
    url = f"https://api.hubapi.com/email/public/v1/campaigns?limit={limit}&amp;amp;offset={offset}"
    response = requests.get(url, headers=headers)

    if response.status_code == 200:
        json_data = json.loads(response.text)
        campaigns = json_data.get('campaigns', [])
        campaign_ids = set([campaign.get('id') for campaign in campaigns])
        campaign_ids_list.update(campaign_ids)  # Add campaign IDs to the set
        print(f"Fetched {len(campaign_ids)} campaigns. Total unique campaigns: {len(campaign_ids_list)}")

        # Check if there are more records
        has_more = json_data.get("hasMore", False)

        if has_more:
            offset = json_data.get("offset", "")
        else:
            break  # Exit the loop when there are no more records
    elif response.status_code == 400:
        print(f"Received status code 400. Skipping offset: {offset}")

        # If you want to continue with the next offset, you can increment it here
        # For example, increment offset by 1:
        # offset = str(int(offset) + 1)
        # Make sure to handle offset conversion and overflow as needed.
    else:
        print(f"Request failed with status code {response.status_code}")
        break  # Exit the loop on any other status code

# Now, you can continue processing the campaign_ids_list or perform any other desired actions.
&lt;/LI-CODE&gt;
&lt;P&gt;&lt;BR /&gt;I've modified the loop to run indefinitely using the condition "while True." This alteration ensures that the loop will persistently process successive requests, even if it encounters a response with a 400 status code. Your ability to adapt the logic within the "elif" block to manage offset increments remains intact. The loop will terminate when it comes across a status code different from 200 or 400, signifying the end of processing.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 20 Sep 2023 08:35:25 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/APIs-Integrations/am-unable-to-filter-response-status-code-400-for-campaigns-data/m-p/852192#M67598</guid>
      <dc:creator>himanshurauthan</dc:creator>
      <dc:date>2023-09-20T08:35:25Z</dc:date>
    </item>
    <item>
      <title>Re: am unable to filter response.status_code == 400 for campaigns data</title>
      <link>https://community.hubspot.com/t5/APIs-Integrations/am-unable-to-filter-response-status-code-400-for-campaigns-data/m-p/852203#M67599</link>
      <description>&lt;P&gt;The above code is working upto 13 iterations than again I got 400 error and loop was stopped&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 20 Sep 2023 08:49:06 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/APIs-Integrations/am-unable-to-filter-response-status-code-400-for-campaigns-data/m-p/852203#M67599</guid>
      <dc:creator>9kumar</dc:creator>
      <dc:date>2023-09-20T08:49:06Z</dc:date>
    </item>
    <item>
      <title>Re: am unable to filter response.status_code == 400 for campaigns data</title>
      <link>https://community.hubspot.com/t5/APIs-Integrations/am-unable-to-filter-response-status-code-400-for-campaigns-data/m-p/852204#M67600</link>
      <description>&lt;P&gt;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/20405"&gt;@himanshurauthan&lt;/a&gt;&amp;nbsp; thanks for sharing code .&lt;/P&gt;&lt;P&gt;For the shared above code it has taken only&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;Fetched 10 campaigns. Total unique campaigns: 10
Fetched 10 campaigns. Total unique campaigns: 20
Fetched 10 campaigns. Total unique campaigns: 30
Fetched 10 campaigns. Total unique campaigns: 40
Fetched 10 campaigns. Total unique campaigns: 50
Fetched 10 campaigns. Total unique campaigns: 60
Fetched 10 campaigns. Total unique campaigns: 70
Fetched 10 campaigns. Total unique campaigns: 80
Fetched 10 campaigns. Total unique campaigns: 90
Fetched 10 campaigns. Total unique campaigns: 100
Fetched 10 campaigns. Total unique campaigns: 110
Fetched 10 campaigns. Total unique campaigns: 120
Fetched 10 campaigns. Total unique campaigns: 130
Received status code 400. Skipping offset: AQAAAYqvLhh+E+yM=
Received status code 400. Skipping offset: AQAAAYqvLhh+E+yM=
Received status code 400. Skipping offset: AQAAAYqvLhhE+yM=
Received status code 400. Skipping offset: AQAAAYqvLhh+E+yM=
Received status code 400. Skipping offset: AQAAAYqvLhh+E+yM=&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;for status 400 it is repeating same offset value not going further&lt;/P&gt;</description>
      <pubDate>Wed, 20 Sep 2023 08:52:32 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/APIs-Integrations/am-unable-to-filter-response-status-code-400-for-campaigns-data/m-p/852204#M67600</guid>
      <dc:creator>9kumar</dc:creator>
      <dc:date>2023-09-20T08:52:32Z</dc:date>
    </item>
    <item>
      <title>Re: am unable to filter response.status_code == 400 for campaigns data</title>
      <link>https://community.hubspot.com/t5/APIs-Integrations/am-unable-to-filter-response-status-code-400-for-campaigns-data/m-p/852210#M67601</link>
      <description>&lt;P&gt;&lt;SPAN&gt;I think your code is repeating the same offset value for status 400 is because maybe you are not checking if the offset value is already in the offset_list. If the offset value is already in the offset_list, then your code will skip the current iteration of the loop and move on to the next iteration.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 20 Sep 2023 09:08:25 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/APIs-Integrations/am-unable-to-filter-response-status-code-400-for-campaigns-data/m-p/852210#M67601</guid>
      <dc:creator>himanshurauthan</dc:creator>
      <dc:date>2023-09-20T09:08:25Z</dc:date>
    </item>
    <item>
      <title>Re: am unable to filter response.status_code == 400 for campaigns data</title>
      <link>https://community.hubspot.com/t5/APIs-Integrations/am-unable-to-filter-response-status-code-400-for-campaigns-data/m-p/852219#M67602</link>
      <description>&lt;P&gt;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/20405"&gt;@himanshurauthan&lt;/a&gt;&amp;nbsp;oh can you help me if you dont mind how to check and get into next iteration please am very new to this&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 20 Sep 2023 09:19:00 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/APIs-Integrations/am-unable-to-filter-response-status-code-400-for-campaigns-data/m-p/852219#M67602</guid>
      <dc:creator>9kumar</dc:creator>
      <dc:date>2023-09-20T09:19:00Z</dc:date>
    </item>
    <item>
      <title>Re: am unable to filter response.status_code == 400 for campaigns data</title>
      <link>https://community.hubspot.com/t5/APIs-Integrations/am-unable-to-filter-response-status-code-400-for-campaigns-data/m-p/852247#M67604</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/20405"&gt;@himanshurauthan&lt;/a&gt;&amp;nbsp;sir , are you there ? can you please help me i got stuck I tried&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 20 Sep 2023 10:38:52 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/APIs-Integrations/am-unable-to-filter-response-status-code-400-for-campaigns-data/m-p/852247#M67604</guid>
      <dc:creator>9kumar</dc:creator>
      <dc:date>2023-09-20T10:38:52Z</dc:date>
    </item>
  </channel>
</rss>

