Hupspot invalid offset

Highlighted
New Contributor

Hi, I am try to get all email campaign through api, I am using recursive loop for retrive all data, but in some point it throw the error (it happens in 14th request) "Invalid offset".

the error is look like . 

Client error: `GET https://api.hubapi.com/email/public/v1/campaigns?hapikey=xxxxxxxxxx&limit=100&offset=AQAAAWjkYoW6AAAAAAPrr+Y=` resulted in a `400 Bad Request` response:\n 
{"status":"error","message":"invalid offset","correlationId":"68973fce-50ff-47ef-96bf-c569d4734ba3","requestId":"6a8f39f (truncated...)\n

I am using recursive loop like 

$rdata = $this->getCompanyID();
        $fdata[] = $rdata['campaigns'];
        $hasMore = $rdata['hasMore'];
        $offset = "&offset={$rdata['offset']}";
        while($hasMore){
            $srdata=$this->getCompanyID($offset);
            $fdata[] = $srdata['campaigns'];
            $hasMore = $srdata['hasMore'];
            $offset = "&offset={$srdata['offset']}";
        }
        return $fdata;
function getCompanyID($offset=NULL){
		$url = "https://api.hubapi.com/email/public/v1/campaigns?hapikey=xxxxxxxxxxxx&limit=100{$offset}";
		$client = new \GuzzleHttp\Client();
		$response = $client->request('GET', $url, [
		    'headers' => [
				'content-type' => 'application/json'
		    ]
		]);
		return json_decode($response->getBody()->getContents(),true);
	}

In solution I read(in here), this is due to "AQAAAWjkYoW6AAAAAAPrr+Y"  plus sign but this offset is auto generate. Why system generate such type of invalid offset.

 

If it is possible to get all data throgh api. Yeah i know per api request limit is 100, but i have need all data.

 

Thanks

Reply
0 Upvotes
3 Replies 3
Highlighted
HubSpot Alumni

Hi @lokendra_jargha , I don't know why we return an offset that you'd need to encode yourself after getting it, but the fix should be that you should URL encode the offset before placing it back into the URL. This is something I'd brought to our development team and something they'll work on in newer versions of that endpoint. In the meantime, you should first URL encode the value, then place it into the URL when using it as the offset parameter.

Highlighted
New Contributor

Hi cbarley,

I am doing encode the offset value as you said but still throw the same error in my end.

Actually what do you mean by encode offset value . I am doing like this 

utf8_encode(offsetValue). 

 Please suggest me the solution.

 

Thanks

Reply
0 Upvotes
Highlighted
HubSpot Alumni

Hi @lokendra_jargha , the call should work if you've encoded it. By "encoding the offset value", I mean URL encoding the value itself. So in your case, the offset value would be AQAAAWjkYoW6AAAAAAPrr%2BY. In JavaScript, I use a function called encodeURIcomponent

 

If you continue to have trouble can you please send me the HubID of the account you're trying to make this request to?

Reply
0 Upvotes