Help with REST API interaction.

--000000000000dff99605a3e1d997
Content-Type: text/plain; charset="UTF-8"

Good day, I've been getting familiarized with Perl for the last couple of
days due to a RT integration project I've been handed. I've also been doing
a lot of research and a lot of asking around in forums for pointers on how
to properly achieve this.

I'm glad to say that I managed to get all my basic scrips up and running,
retrieving all the values I needed and updating all the fields I wanted to
update. I now need to get to the part of actually interacting with the
external RESTful API.

If what I understood from my research is correct, I will be working with
the REST::Client and JSON modules. And what I need to do is:

   1. Encode the values I want to pass over to the API into a JSON object.
   2. Do some coding on the other side in order to achieve my desired
   funcionalities and pass some values back to RT (for this I won't need your
   help since the coding won't be done in Perl, sadly)
   3. Decode the response I get back to Perl and use those values in RT to
   do some more updating.

I would greatly appreciate if you could provide me with some pointers and
code examples of this being done. Choosing the values I need to pass,
encoding them, passing them over to the API, and then decoding the response.

Needless to say, I am documenting all my work and will gladly share my
experience in this project and all the coding work in any forum.

--000000000000dff99605a3e1d997
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div>Good day, I&#39;ve been getting familiarized with Per=
l for the last couple of days due to a RT integration project I&#39;ve been=
 handed. I&#39;ve also been doing a lot of research and a lot of asking aro=
und in forums for pointers on how to properly achieve this.<br><br></div>I&=
#39;m glad to say that I managed to get all my basic scrips up and running,=
 retrieving all the values I needed and updating all the fields I wanted to=
 update. I now need to get to the part of actually interacting with the ext=
ernal RESTful API. <br><br><div>If what I understood from my research is co=
rrect, I will be working with the REST::Client and JSON modules. And what I=
 need to do is: <br></div><div><ol><li>Encode the values I want to pass ove=
r to the API into a JSON object.</li><li>Do some coding on the other side i=
n order to achieve my desired funcionalities and pass some values back to R=
T (for this I won&#39;t need your help since the coding won&#39;t be done i=
n Perl, sadly)</li><li>Decode the response I get back to Perl and use those=
 values in RT to do some more updating.</li></ol><div>I would greatly appre=
ciate if you could provide me with some pointers and code examples of this =
being done. Choosing the values I need to pass, encoding them, passing them=
 over to the API, and then decoding the response.</div><div><br></div><div>=
Needless to say, I am documenting all my work and will gladly share my expe=
rience in this project and all the coding work in any forum.<br></div></div=
></div>

--000000000000dff99605a3e1d997--
0
franciscoo
4/22/2020 2:21:31 PM
perl.beginners 29388 articles. 4 followers. Follow

2 Replies
287 Views

Similar Articles

[PageSpeed] 40
Get it on Google Play
Get it on Apple App Store

--hmq3ifx5jyfq562x
Content-Type: text/plain; charset=utf-8
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

Hi Francisco,

2020-04-22 11:21:31 -0300 Francisco Acu=C3=B1a:
> Good day, I've been getting familiarized with Perl for the last couple of
> days due to a RT integration project I've been handed. I've also been doi=
ng
> a lot of research and a lot of asking around in forums for pointers on how
> to properly achieve this.
>=20
> I'm glad to say that I managed to get all my basic scrips up and running,
> retrieving all the values I needed and updating all the fields I wanted to
> update. I now need to get to the part of actually interacting with the
> external RESTful API.
>=20
> If what I understood from my research is correct, I will be working with
> the REST::Client and JSON modules. And what I need to do is:
>=20
>    1. Encode the values I want to pass over to the API into a JSON object.
>    2. Do some coding on the other side in order to achieve my desired
>    funcionalities and pass some values back to RT (for this I won't need =
your
>    help since the coding won't be done in Perl, sadly)
>    3. Decode the response I get back to Perl and use those values in RT to
>    do some more updating.
>=20
> I would greatly appreciate if you could provide me with some pointers and
> code examples of this being done. Choosing the values I need to pass,
> encoding them, passing them over to the API, and then decoding the respon=
se.
>=20
> Needless to say, I am documenting all my work and will gladly share my
> experience in this project and all the coding work in any forum.

One way to do it is like this:

use strict;
use warnings;
use feature 'say';
use LWP::UserAgent;
use JSON;

my $ua =3D LWP::UserAgent->new;
my $uri =3D 'https://example.com/path/to/something';
my %headers =3D ('Content-Type' =3D> 'application/json');

# here are the values to pass to the API
my %values =3D (number =3D> 1, name =3D> 'etc');

my $res =3D $ua->post(
    $uri,
    %headers,
    Content =3D> JSON::encode_json(\%values)
);

if ($ua->is_success) {
    # if you're receiving a JSON
    my $data =3D JSON::decode_json $res->content;
    # now $data is a Perl data structure; it's the decoded JSON

    # but if you're receiving (say) HTML
    say $res->decoded_content;
}
else {
    # handle errors here
}

See these docs for the modules' details:
https://metacpan.org/pod/LWP::UserAgent
https://metacpan.org/pod/JSON

--hmq3ifx5jyfq562x
Content-Type: application/pgp-signature; name="signature.asc"

-----BEGIN PGP SIGNATURE-----

iQIzBAABCAAdFiEE76bA2CIEBbRIkVojESeEcgcg4LoFAl6ja1gACgkQESeEcgcg
4LpaLhAAmmGfukTwxWgIPFzh9gJXfuK0fjRvQHE1Cub82uqQmt4vAv8M+67IioL/
BAza4wRVkn+do1aQC53w4RY/f6WO1jjybCKssLsUWmoTxXHxJYbB3OBg26WtjJKB
a4wzh/4tGrGoFD/8xzB/tYZ82PJ4dE3htxn6EcMQ1KdZqvIlS+uOCexhUDLITISu
nFSktEHRL/OvXLRaIX2B9LBhpWdp2MRBvW6+AGQaupb2gUB5Ly2CVDCm1RcmA15N
vuBzs+ZwyxujWEbOXaljoU4OOhoki3KZkbuHkKeirOAcdX6pA4gUcEmAIWCbXLNn
1yGjPZ+Tm1r0A01G7Vexex6D60i4vbmIPf7mVd7CddwJjNwWPX5YOtcPQ9qE2gCv
QILG3Df+excaZmPH07+uFx9Otn97Jsti5X5C3PNrMVZJ/MOqp3K0kYaX6qPM6P2k
+z2E7jweqzq5xnvRWfnuGWg7TEMGK8bBPADsSG5KFOpvXWU9BoIaNMm106u5+KSX
Y8k728DPpBNJzAHiB63Qx7fU3MODdipxizE42kJghOxNQSfD/bMf53nuYBmnQNQj
z14AIASrmUsJ7XGYF+qQDm8iEmfhJs8r4rdzx3G8srrG50fvzKCyYwn/Lb5QbUFH
wbtAB1kJXg9yT54gHcMSmb7S7pV7bksL49PXI2dJvT4wW11azb0=
=Zaru
-----END PGP SIGNATURE-----

--hmq3ifx5jyfq562x--
0
gilmagno
4/24/2020 10:42:33 PM
--jrbocvzir5hpunxu
Content-Type: text/plain; charset=utf-8
Content-Disposition: inline

Correction: we should use $res->is_success in the 'if' below, not
$ua->is_success.

2020-04-24 19:42:32 -0300 Gil Magno:
> if ($ua->is_success) {

--jrbocvzir5hpunxu
Content-Type: application/pgp-signature; name="signature.asc"

-----BEGIN PGP SIGNATURE-----

iQIzBAABCAAdFiEE76bA2CIEBbRIkVojESeEcgcg4LoFAl6jbIAACgkQESeEcgcg
4LoD4hAAzlONrv81DLzkISgSnZVI1WDwIX4kgfxf6NYZt+uuecSlof9NqydWL08e
AXGbd04ffqOzywxye/bPt0zbcU9VLwdxvFokKdqMs3DxwKcVnBP75SR8hdsOsOsL
TeasiwNJ4dKyzdV/fM2SUPsv8uOl0apqj0muEvInMK559ZqWRqfsDSqj/2ZE6Is0
tpfxnOjr2tbkxqtR5Nr4wtWr9Ns2OTGAKM+0DlHJU9GQCNyrDf0OMmqyq/Ck8pDV
SPVJEflM3CYMjHpXriEyNfSGZqD6sV0HDBbhxsGo744JngD5a/YwnPQ8T77YEzaK
NewAWUv/kVZY4+Ar6oVk7xNpIVgakKbl6K43FB9yqiwweoRd8bL3TumZ6b4tWtte
q8shRRvCp8A/+asiqTc5VwNKLv2iJPdhpqs3u4Ylqu2kLmcRMW/XzRkxQ+oinZK6
dxEMGmg19eYpcktUyurxxf0RHdn8Mxni3l0/88qlphyamhYRgZFQEYLOHZ76VGxe
DzBO78aByYPHLFDTWwvX+EaNxysX/P+eG5ejCOuYx8AYonJATIGTn5KIhd5ik8Sc
thA6lq1l0yNCPBqGM+SlX+YVuEu8Da1j0WPduwKTq01NcGabhZ5lUrXX93lA17Id
1Wy9G2xKTQscXSD0Cft03lU67BaFk0xDyNX+6WT0pUrQcw1JeMY=
=RjIh
-----END PGP SIGNATURE-----

--jrbocvzir5hpunxu--
0
gilmagno
4/24/2020 10:47:29 PM
Reply: