Zendesk REST API tutorial - Perl edition Follow

Comments

15 comments

  • Avatar
    John Kinsella

    I can get the examples above to work if I use my web password, but when I try to use an API token I get a 401 error. Not sure what I need to change?

  • Avatar
    Charles Nadeau

    Hi John,

    Have you appended the string '/token' to your email address?

    my $uname = 'example@address.com/token';
    my $pass = 'your\_api\_token';

    For more info, see  [Security and Authentication](http://developer.zendesk.com/documentation/rest_api/introduction.html#security-and-authentication) in the API docs. The following authentication format is used when using the API token:

    `{email_address}/token:{api_token}`

     

  • Avatar
    John Kinsella

    Hey Charles -

    Ah! I was putting the token: string as part of the password in the password field.  I mis-intreperted the delimiters.

     

    That did the trick, thanks!

  • Avatar
    Brandon Clark

    How can I pass a value from an HTML form into my perl (PL) file? I am very new to this and stumped. Any help would be greatly appreciated!

  • Avatar
    Charles Nadeau

    Hi Brandon,

    Passing HTML form values to Perl scripts is traditionally done with Perl CGI scripts on the web server (http://www.tutorialspoint.com/perl/perl_cgi.htm). Depending on what you're trying to do, another option is using a Perl web framework like Mojolicious (http://mojolicio.us/) or Catalyst (http://www.catalystframework.org/).

  • Avatar
    Conrad

    Hi Charles,

    I changed the first example (getting data) so that I can get data from tickets. However, I'm only able to get data from specific fields one at a time. I can't for example get the ticket id and subject together. How do I add the {"id"} and {"subject"} etc together?

    Thanks,
    Conrad

  • Avatar
    Charles Nadeau

    Hi Conrad,

    The response has all the fields for each ticket, so you can refer to any additional ticket fields in the for loop as follows:

    ```
    my @tickets = @{ $data->{'tickets'} };
    foreach my $ticket ( @tickets ) {
    print $ticket->{"id"} . " " . $ticket->{"subject"} . "\n";
    }
    ```

    You can also use a double-quoted string to interpolate (or insert directly) the values to format the results more cleanly:

    `print "ID: $ticket->{'id'} Subject: $ticket->{'subject'}\n";`

    You probably have a lot more tickets than groups and the API only returns a limited number of results at a time, so make sure to include some code to paginate through all your tickets. See this article: https://support.zendesk.com/hc/en-us/articles/204232743-Paginating-through-lists.

  • Avatar
    Conrad

    Hi Charles,

    Thanks for the reply. It worked.

    I'm having another problem now though when writing the data to a file. It works up to where I add the "priority" field:

    my @tickets = @{ $data->{'tickets'} };
    my $output;
    foreach my $tickets ( @tickets ) {
    $output .= $tickets->{"id"} . " " . $tickets->{"status"} . " " . $tickets->{"group_id"} . " " . $tickets->{"subject"} . " " . $tickets->{"requester_id"} . " " . $tickets->{"created_at"} . " " . $tickets->{"type"} . " " . $tickets->{"assignee_id"} . " " . $tickets->{"updated_at"} . " " . $tickets->{"priority"} . "\n"; # add each name to the $output variable
    }

    open my $fh, '>', 'tickets.txt'
    or die "Could not open file $!";
    print $fh $output;
    close $fh;

    print "Done.\n";

    Then it gives me the following error:
    Use of uninitialized value in concatenation (.) or string at get_tickets 7.pl line 34.

    Line 34 is the line were I put all the fields I need.

  • Avatar
    Charles Nadeau

    I notice you're using two variables with the same identifier, `@tickets` and `$tickets`, to do two different things. Perl may be getting confused by the context they appear in. Try renaming your scalar `$tickets` variable to `$ticket` (singular) everywhere.

  • Avatar
    Charles Nadeau

    @conrad It occurs to me another thing might be going on. Some ticket properties can be null, which Perl interprets as undefined. For example, if no agent is assigned to a ticket, then `assignee_id` is null. That would cause an "uninitialized value" error when trying to add it to a string.

    You can add a small utility function at the bottom of the file to check if each value is undefined, and if so, return an empty string for the string concatenation. Here's a possible version of such a function:

    ```
    sub check_undef {
    my ($value) = @_;
    if (defined $value) {
    $value;
    } else {
    "";
    }
    }
    ```

    In your statement that concatenates ticket values to the $output variable, you'd pass the values that might return null to the function. (Some properties like id and status always have a value so it wouldn't be necessary). Example:

    ```
    $output .= $ticket->{"id"} . " " . &check_undef($ticket->{"group_id"}) . " " . ...
    ```

    The function would return either a value if it exists or an empty string.

  • Avatar
    Conrad

    Charles, you are a super start! It worked. Thank you so much.

    My next task is to sort out the paginating. I'll try my best to figure this out myself.

    Thanks again.

  • Avatar
    Conrad

    Ok so I managed to get the paginating to work, but now it overwrites the saved file on each loop. So I only get the data of the last loop. How do I get it to write the information from each loop into one file?

  • Avatar
    Conrad

    I figured out that I need the change "open my $fh, ">", 'tickets.csv'" to "open my $fh, ">>", 'tickets.csv'". Only problem now is that it keeps adding data when running the file again. I need it to overrite on the first loop, but append after that.

  • Avatar
    Charles Nadeau

    You shouldn't have to change ">" to ">>", which keeps appending content to the file every time you run the script rather than creating a new one. Instead, append all the results to your $output variable and then print that out to the file. Make sure you declare the $output variable before the loop starts to prevent overwriting its contents, and then print it to your file after exiting the loop. Example:

    ```
    ...
    my $ua = LWP::UserAgent->new(ssl_opts =>{ verify_hostname => 0 });
    my $output;

    while ($url) {
    ... # append tickets to the $output string until no more tickets left to paginate
    }

    open my $fh, '>', 'tickets.txt' or die "Could not open file $!";
    print $fh $output;
    close $fh;
    ```

  • Avatar
    Conrad

    Thanks Charles. Sorted.

Please sign in to leave a comment.

Powered by Zendesk