#!/usr/bin/perl use strict; use LWP::UserAgent; use LWP::Debug qw(+); use JSON; # Zendesk my $netloc = 'xxx.zendesk.com:443'; my $realm = 'Web Password'; my $uname = 'me@mycomp.com/token'; my $pass = 'xxxxxxxxxxx'; my $Server = $ARGV[0]; my $TicketID = "1234"; Comment_Zendesk($TicketID,$Server); exit; sub Comment_Zendesk { my($ZID,$ZServer) = @_; # Create a user agent, and specify the credentials and header of your API requests my $ua = LWP::UserAgent->new(); $ua->credentials($netloc, $realm, $uname, $pass); $ua->default_header('Content-Type' => 'application/json'); # Endpoint URL my $url = 'https://xxx.zendesk.com/api/v2/tickets/'.$ZID.'.json'; # Call the API my $body = "Message posted from $ZServer"; my $json = JSON->new->allow_nonref; my $json_body = $json->encode($body); my $data = '{"ticket": { "comment": {"body": ' . $json_body . ' } } }'; my $response = $ua->put($url, Content => $data); # Check for HTTP error codes if (! $response->is_success) { die 'http status: ' . $response->code . ' ' . $response->message; } print "\n--- response->code ---\n" . $response->code . "\n"; print "\n--- response->message ---\n" . $response->message. "\n"; print "\n--- response->content ---\n" . $response->content() . "\n"; print "\n--- response->header ---\n" . $response->header("WWW-Authenticate"); }