API Not Returning "Link" In Header

Hi,

I am probably missing the obvious, so go easy on me!

We are fairly new to Fresh, and I am trying to pull back all the tickets this month using the API (via Python). But it has pulling only 30 tickets. As this is a nice clean number, I was assuming pagination, however looking in the headers there is no “Link”.

I am expecting about 1300 rows to be returned for this month.

The URL/Query being used…

https://domain-here.freshservice.com/api/v2/tickets/filter?query="created_at:>'2023-01-31' AND created_at:<'2023-03-01'"

Removing the date element, “Link” appears in the response headers, and I get back over 1300 rows.

I am not getting any errors, or messages regarding Rate Limiting.

Has something changed, or am I missing the obvious?

Thanks in Advance!

hi @notreallyhere

If you add the per_page and page query parameters to the api call you can pull back up to 100 at at a time. This should also then mean the link to the next page will appear in the header.

1 Like

Looks like Rob found the root cause and once you add the page and per_page (100 max) you can implement pagination in the code:

https://domain-here.freshservice.com/api/v2/tickets/filter?query=%22created_at%3a%3e%272023-01-31%27+AND+created_at%3a%3c%272023-03-01%27%22&page=1&per_page=100

Thank you both for your replies - @RobAtOpinyin / @rasimm .

I have not had chance to test this out today, but hope to tomorrow.

I will update once I get a result!

Again, thank you both.

Again thanks - the “page” and “per_page” does appear to have done the trick, although the header is still not showing a “Link” entry, so I will rewrite my code to increment “page” until no data is returned (an empty data set).

Hi Jon,

Building a Powershell wrapper, but not done too much Python. In Powershell, you have to use certian commands to get the detailed header and even the verbose API errors returned, not sure if there is something comparable in Python. Here is something you can try and emulate in Python. Note that the link is only if there are more records than the page.

function Get-Ticket {
    [CmdletBinding()]
    param (
        $tenant,
        $api_key,
        $page = 1,
        $per_page = 100
    )
    begin {}
    process {
        try {
            $Headers = @{"Authorization" = ("Basic {0}" -f [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $api_key,$null))))}

            $uri = ('https://{0}.freshservice.com/api/v2/tickets?page=1&per_page=5' -f $tenant,$page,$per_page)

            $results = do {

                $params = @{
                    Headers     = $Headers
                    Uri         = $uri
                    Method      = 'GET'
                }

                [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
                $result = Invoke-WebRequest @params

                ($result.Content | ConvertFrom-Json).tickets

                Write-Verbose -Message ("Next link is {0}" -f $result.Headers.Link)

                if ($result.Headers.Link) {
                    $uri = [regex]::Matches($result.Headers.Link,'<(?<Uri>.*)>')[0].Groups['Uri'].Value
                }
            }
            until (!$result.Headers.Link)
        }
        catch {
            Throw $_
        }
    }
    end {
        $results
    }
}

$apiParams = @{
    tenant  = 'TENANT_NAME'
    api_key = 'API_KEY'
}

$t = Get-Ticket @apiParams -Verbose
$t

Output:

VERBOSE: HTTP/1.1 GET with 0-byte payload
VERBOSE: received -byte response of content type application/json
VERBOSE: Next link is <https://TENANT_NAME.freshservice.com/api/v2/tickets?page=2&per_page=5>; rel="next"    
VERBOSE: HTTP/1.1 GET with 0-byte payload
VERBOSE: received -byte response of content type application/json
VERBOSE: Next link is <https://TENANT_NAME.freshservice.com/api/v2/tickets?page=3&per_page=5>; rel="next"    
VERBOSE: HTTP/1.1 GET with 0-byte payload
VERBOSE: received -byte response of content type application/json
VERBOSE: Next link is <https://TENANT_NAME.freshservice.com/api/v2/tickets?page=4&per_page=5>; rel="next"    
VERBOSE: HTTP/1.1 GET with 0-byte payload
VERBOSE: received -byte response of content type application/json
VERBOSE: Next link is <https://TENANT_NAME.freshservice.com/api/v2/tickets?page=5&per_page=5>; rel="next"    
VERBOSE: HTTP/1.1 GET with 0-byte payload
VERBOSE: received -byte response of content type application/json
VERBOSE: Next link is <https://TENANT_NAME.freshservice.com/api/v2/tickets?page=6&per_page=5>; rel="next"    
VERBOSE: HTTP/1.1 GET with 0-byte payload
VERBOSE: received -byte response of content type application/json
VERBOSE: Next link is      

Thanks for your inputs @RobAtOpinyin @notreallyhere!

@rasimm The link response header will only be present if there are further “pages” of data based on the current request. You can treat the absence of the link response header to mean that all possible data have been provided in the current response.

Hi,

This was the challenge, I am not getting a “Link” entry when I knew there were multiple pages. Since requesting specific pages (“page=n”) I can pull back all the data until no data is returned.

This topic was automatically closed 6 days after the last reply. New replies are no longer allowed.