diff --git a/README.md b/README.md index ea32f2fe..4a51f630 100644 --- a/README.md +++ b/README.md @@ -104,9 +104,9 @@ The following methods are available: - $mess->decoded\_content( %options ) Returns the content with any `Content-Encoding` undone and for textual content - the raw content encoded to Perl's Unicode strings. If the `Content-Encoding` - or `charset` of the message is unknown this method will fail by returning - `undef`. + (text/*, XML, JSON, or JavaScript) the raw content encoded to Perl's Unicode + strings. If the `Content-Encoding` or `charset` of the message is unknown this + method will fail by returning `undef`. The following options can be specified. diff --git a/lib/HTTP/Headers.pm b/lib/HTTP/Headers.pm index 1224de4e..67a02f11 100644 --- a/lib/HTTP/Headers.pm +++ b/lib/HTTP/Headers.pm @@ -405,6 +405,20 @@ sub content_is_xml { return 0; } +sub content_is_json { + my $ct = shift->content_type; + # text/json is not standard but still used by various servers. + # No issue including it as well. + return $ct eq 'application/json' || $ct eq 'text/json' || $ct =~ /\+json$/; +} + +sub content_is_javascript { + my $ct = shift->content_type; + # text/javascript is obsolete in RFC4329 but still used. + # No issue including it as well. + return $ct eq 'application/javascript' || $ct eq 'text/javascript'; +} + sub referer { my $self = shift; if (@_ && $_[0] =~ /#/) { @@ -737,6 +751,16 @@ content is XHTML. This method can't be used to set Content-Type. Returns TRUE if the Content-Type header field indicate that the content is XML. This method can't be used to set Content-Type. +=item $h->content_is_json + +Returns TRUE if the Content-Type header field indicate that the +content is JSON. This method can't be used to set Content-Type. + +=item $h->content_is_javascript + +Returns TRUE if the Content-Type header field indicate that the +content is JavaScript. This method can't be used to set Content-Type. + =item $h->content_encoding The Content-Encoding header field is used as a modifier to the diff --git a/lib/HTTP/Message.pm b/lib/HTTP/Message.pm index abffb09e..af1c6e12 100644 --- a/lib/HTTP/Message.pm +++ b/lib/HTTP/Message.pm @@ -351,7 +351,7 @@ sub decoded_content } } - if ($self->content_is_text || (my $is_xml = $self->content_is_xml)) { + if ($self->content_is_text || (my $is_xml = $self->content_is_xml) || $self->content_is_json || $self->content_is_javascript) { my $charset = lc( $opt{charset} || $self->content_type_charset || @@ -879,9 +879,9 @@ for details about how charset is determined. =item $mess->decoded_content( %options ) Returns the content with any C undone and for textual content -the raw content encoded to Perl's Unicode strings. If the C -or C of the message is unknown this method will fail by returning -C. +(text/*, XML, JSON, or JavaScript) the raw content encoded to Perl's Unicode +strings. If the C or C of the message is unknown +this method will fail by returning C. The following options can be specified.