Skip to content

Commit 748e046

Browse files
committed
Allow decoding of application/json.
This will detect if the file is UTF-8, UTF-16, or UTF-32, and try and return the content decoded. It will allow use of the charset/ default_charset options. Also allow text/json (if UTF-8).
1 parent 8f8840d commit 748e046

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

lib/HTTP/Headers.pm

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,13 @@ sub content_is_xml {
405405
return 0;
406406
}
407407

408+
sub content_is_json {
409+
my $ct = shift->content_type;
410+
# text/json is not standard but still used by various servers.
411+
# No issue including it as well.
412+
return $ct eq 'application/json' || $ct eq 'text/json' || $ct =~ /\+json$/;
413+
}
414+
408415
sub referer {
409416
my $self = shift;
410417
if (@_ && $_[0] =~ /#/) {
@@ -737,6 +744,11 @@ content is XHTML. This method can't be used to set Content-Type.
737744
Returns TRUE if the Content-Type header field indicate that the
738745
content is XML. This method can't be used to set Content-Type.
739746
747+
=item $h->content_is_json
748+
749+
Returns TRUE if the Content-Type header field indicate that the
750+
content is JSON. This method can't be used to set Content-Type.
751+
740752
=item $h->content_encoding
741753
742754
The Content-Encoding header field is used as a modifier to the

lib/HTTP/Message.pm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ sub decoded_content
351351
}
352352
}
353353

354-
if ($self->content_is_text || (my $is_xml = $self->content_is_xml)) {
354+
if ($self->content_is_text || (my $is_xml = $self->content_is_xml) || $self->content_is_json) {
355355
my $charset = lc(
356356
$opt{charset} ||
357357
$self->content_type_charset ||

0 commit comments

Comments
 (0)