LWP-JSON-Tiny

 view release on metacpan or  search on metacpan

docs/HTTP-Request-JSON.html  view on Meta::CPAN

>SYNOPSIS</a></h1>

<pre> my $request = HTTP::Request::JSON-&#62;new(PATCH =&#62; &#34;$base_url/death_ray&#34;);
 # $request has an Accept header saying it&#39;s OK to send JSON back
 $request-&#62;json_content(
     {
         self_destruct_mechanism   =&#62; &#39;disabled&#39;,
         users_allowed_to_override =&#62; [],
     }
 );
 # Request content is JSON-encoded, and the content-type is set.</pre>

<h1><a class='u' href='#___top' title='click to go to top of document'
name="DESCRIPTION"
>DESCRIPTION</a></h1>

<p>This is a simple subclass of HTTP::Request that does two things. First of all, it sets the Accept header to <code>application/json</code> as soon as it&#39;s created. Secondly, it implements a <a href="#json_content" class="podlinkpod"
>&#34;json_content&#34;</a> method that adds the supplied data structure to the request, as JSON, or returns the current JSON contents as a Perl structure.</p>

<h2><a class='u' href='#___top' title='click to go to top of document'
name="new"

docs/LWP-JSON-Tiny.html  view on Meta::CPAN


 my $other_response = $some_other_object-&#62;do_stuff(...);
 if (LWP::UserAgent::JSON-&#62;rebless_maybe($other_response)) {
     do_something($other_response-&#62;json_content);
 }</pre>

<h1><a class='u' href='#___top' title='click to go to top of document'
name="DESCRIPTION"
>DESCRIPTION</a></h1>

<p>A lot of RESTful API integration involves pointless busy work with setting accept and content-type headers, remembering how Unicode is supposed to work and so on. This is a very simple wrapper around HTTP::Request and HTTP::Response that handles a...

<p>There are four classes in this distribution:</p>

<dl>
<dt><a name="LWP::JSON::Tiny"
>LWP::JSON::Tiny</a></dt>

<dd>
<p>Pulls in the other classes, and implements a <a href="#json_object" class="podlinkpod"
>&#34;json_object&#34;</a> method which returns a JSON object, suitable for parsing and emitting JSON.</p>

<dt><a name="HTTP::Request::JSON"
>HTTP::Request::JSON</a></dt>

<dd>
<p>A subclass of HTTP::Request. It automatically sets the Accept header to <code>application/json</code>, and implements a <a href="http://search.cpan.org/perldoc?HTTP%3A%3ARequest%3A%3AJSON#json_content" class="podlinkpod"
>json_content</a> method which takes a JSONable data structure and sets the content-type.</p>

<dt><a name="HTTP::Response::JSON"
>HTTP::Response::JSON</a></dt>

<dd>
<p>A subclass of HTTP::Response. It implements a <a href="http://search.cpan.org/perldoc?HTTP%3A%3AResponse%3A%3AJSON#json_content" class="podlinkpod"
>json_content</a> method which decodes the JSON contents into a Perl data structure.</p>

<dt><a name="LWP::UserAgent::JSON"
>LWP::UserAgent::JSON</a></dt>

<dd>
<p>A subclass of LWP::UserAgent. It does only one thing: is a response has content-type JSON, it reblesses it into a HTTP::Response::JSON object. It exposes this method <a href="http://search.cpan.org/perldoc?LWP%3A%3AUserAgent%3A%3AJSON#rebless_mayb...
>rebless_maybe</a> for convenience, if you ever get an HTTP::Response object back from some other class.</p>
</dd>
</dl>

<p>As befits a ::Tiny distribution, sensible defaults are applied. If you really need to tweak this stuff (e.g. you really care about the very slight performance impact of sorting all hash keys), look at the individual modules&#39; documentation for ...

<h2><a class='u' href='#___top' title='click to go to top of document'
name="Class_methods"
>Class methods</a></h2>

lib/HTTP/Request/JSON.pm  view on Meta::CPAN

=head1 SYNOPSIS

 my $request = HTTP::Request::JSON->new(PATCH => "$base_url/death_ray");
 # $request has an Accept header saying it's OK to send JSON back
 $request->json_content(
     {
         self_destruct_mechanism   => 'disabled',
         users_allowed_to_override => [],
     }
 );
 # Request content is JSON-encoded, and the content-type is set.

=head1 DESCRIPTION

This is a simple subclass of HTTP::Request that does two things.
First of all, it sets the Accept header to C<application/json> as soon
as it's created. Secondly, it implements a L</json_content>
method that adds the supplied data structure to the request, as JSON,
or returns the current JSON contents as a Perl structure.

=head2 new

lib/LWP/JSON/Tiny.pm  view on Meta::CPAN

 }

 my $other_response = $some_other_object->do_stuff(...);
 if (LWP::UserAgent::JSON->rebless_maybe($other_response)) {
     do_something($other_response->json_content);
 }

=head1 DESCRIPTION

A lot of RESTful API integration involves pointless busy work with setting
accept and content-type headers, remembering how Unicode is supposed to work
and so on. This is a very simple wrapper around HTTP::Request and
HTTP::Response that handles all of that for you.

There are four classes in this distribution:

=over

=item LWP::JSON::Tiny

Pulls in the other classes, and implements a L</"json_object"> method which
returns a JSON object, suitable for parsing and emitting JSON.

=item HTTP::Request::JSON

A subclass of HTTP::Request. It automatically sets the Accept header to
C<application/json>, and implements a
L<json_content|HTTP::Request::JSON/json_content> method
which takes a JSONable data structure and sets the content-type.

=item HTTP::Response::JSON

A subclass of HTTP::Response. It implements a
L<json_content|HTTP::Response::JSON/json_content> method which
decodes the JSON contents into a Perl data structure.

=item LWP::UserAgent::JSON

A subclass of LWP::UserAgent. It does only one thing: is a response has
content-type JSON, it reblesses it into a HTTP::Response::JSON object.
It exposes this method L<rebless_maybe|LWP::UserAgent::JSON/rebless_maybe>
for convenience, if you ever get an HTTP::Response object back from some
other class.

=back

As befits a ::Tiny distribution, sensible defaults are applied. If you really
need to tweak this stuff (e.g. you really care about the very slight
performance impact of sorting all hash keys), look at the individual
modules' documentation for how you can subclass behaviour.

lib/LWP/UserAgent/JSON.pm  view on Meta::CPAN


    # Obviously, if the object isn't blessed yet, it doesn't make sense
    # to rebless it.
    return 0 if !Scalar::Util::blessed($object);

    # If the object doesn't have a content_type method, maybe that's because
    # it doesn't have one *yet*?
    # HTTP::Message is known to build methods like this via an AUTOLOAD,
    # on demand, so if e.g. this was the response to a GET request where
    # there was no explicit content type set in the request, and we hadn't
    # done any content-type stuff in the same process previously, this will
    # be the first time anyone has even tried to call this method.
    # So see if we can trigger the creation of this method.
    if (!$object->can('content_type')) {
        if ($object->isa('HTTP::Message')) {
            eval {
                $object->content_type;
            }
        }
    }
    return 0 if !$object->can('content_type');

t/request.t  view on Meta::CPAN

sub encode_valid {
    my $request = $tested_class->new;
    is($request->content_type, '', 'No content type at first');
    $request->json_content({foo => ['foo', 'bar', { baz => 'bletch'}]});
    is(
        $request->decoded_content,
        '{"foo":["foo","bar",{"baz":"bletch"}]}',
        'Simple JSON encoding worked'
    );
    is($request->content_type, 'application/json',
        'We have a content-type now');
}

sub encode_unicode {
    return if $^V lt v5.13.8;
    use if $^V ge v5.13.8, feature => 'unicode_strings';

    # OK, time to try the most famous Unicode character of all,
    # PILE OF POO.
    # Unicode: U+1F4A9 (U+D83D U+DCA9), UTF-8: F0 9F 92 A9
    my $pile_of_poo = "\x{1f4a9}";

t/user_agent.t  view on Meta::CPAN

            . '],'
            . '"punchline":"...it was cheese",'
            . '"setup":"A guy walks into a bar, but very quickly...",'
            . '"title":"Shaggy dog story"}'
            . ']'
            . "\n"
    );

    # If Content and Content-Type are specified normally, they happen.
    response_matches(
        'You could always specify content and content-type',
        sub {
            $user_agent->post(
                'record::rickroll.wtf',
                'Content-Type' => 'music/midi',
                Content        => 'Will give you up at some point after all'
                )
        },
        <<FORM_RESPONSE
POST record::rickroll.wtf
User-Agent: TestStuff



( run in 2.252 seconds using v1.01-cache-2.11-cpan-524268b4103 )