http-get-json-to-yaml.t 856 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. #!/usr/bin/perl
  2. =pod
  3. DEPS:
  4. * Make sure to install JSON::XS and YAML::XS, probably with cpan.
  5. NOTE:
  6. * for each arg of type URL http request get a application/json
  7. * convert the json to yaml and print out
  8. =cut
  9. use LWP::UserAgent;
  10. use JSON::XS;
  11. use YAML::XS;
  12. my $ua = LWP::UserAgent->new;
  13. foreach my $arg (@ARGV) {
  14. # print $arg, "\n";
  15. my $server_endpoint = $arg;
  16. my $req = HTTP::Request->new(GET => $server_endpoint);
  17. $req->header('content-type' => 'application/json');
  18. my $resp = $ua->request($req);
  19. if ($resp->is_success) {
  20. {
  21. local $YAML::XS::Boolean = "JSON::PP";
  22. print Dump (JSON::XS->new->utf8->decode ($resp->decoded_content));
  23. }
  24. }
  25. else {
  26. print "HTTP GET error code: ", $resp->code, "\n";
  27. print "HTTP GET error message: ", $resp->message, "\n";
  28. }
  29. }