humbleCoder has asked for the wisdom of the Perl Monks concerning the following question:

I am writing a script and I am trying to write a file from a url that is in the form of a .json. I need to basically get the file from a url to my home directory on a linux server. I am getting this json from WWW.trello.com. I am trying to get the whole board to export as a .json. The site uses RESTful api and I don't know how to get the file from a url, then write the information it displays to a file.

Replies are listed 'Best First'.
Re: API Calls with RESTful & Trello
by roboticus (Chancellor) on Jul 20, 2014 at 23:03 UTC

    humbleCoder:

    If you're on a *nix box, you could use a tool like curl or wget if you're just doing a one-off. If you want to write some perl code, or are planning on doing something with the data in perl after fetching it, you might want to use LWP::UserAgent to get the URL, then write the data to a file.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      I have tried wget and curl but the issue I run into is that the website requires credentials to veiw the url.
Re: API Calls with RESTful & Trello
by Your Mother (Archbishop) on Jul 21, 2014 at 03:52 UTC

    :P Go through the docs more slowly: https://trello.com/docs/gettingstarted/index.html. This should be fun to mess around with if you have the right packages: strictures, URI::QueryParam, JSON, YAML (not necessary, comment out if you like), Path::Tiny. Set your developer key in your environment or add to the script.

    #!/usr/bin/env perl use 5.010; use strictures; use URI; use URI::QueryParam; use LWP::UserAgent; use JSON; use YAML; # Just for fun/readability. use Path::Tiny; use open qw( :encoding(UTF-8) :std ); $ENV{TRELLO_KEY} ||= "YouForgotToSetThis"; my $version = 1; # My guess/interpretation of URL spec. my $service = URI->new("https://api.trello.com/"); $service->query_param( key => $ENV{TRELLO_KEY} ); my $uri = $service->clone; # URI from docs -> https://trello.com/docs/gettingstarted/index.html $uri->path("/$version/board/4d5ea62fd76aa1136000000c"); # Uncomment to get TONS of data -> $uri->query_param( cards => "open" +); $uri->query_param( list_fields => "name,desc" ); my $response = LWP::UserAgent ->new ->get( $uri ); die $response->as_string unless $response->is_success; my $data = decode_json( $response->decoded_content ); print YAML::Dump( $data ); # Just to see in human-readable tree. my $outfile = path("ohai.json"); # Save the JSON to a file. $outfile->spew_utf8( $response->decoded_content ); say "Check $outfile for the JSON."; exit 0;