-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixed JSON request had 1 or 0 array elements and add json_parameters method #21
Changes from all commits
6ead935
12b5a42
03d639f
2d6e09b
3e9347e
2c612dc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -513,6 +513,35 @@ validate parameters using L<Kossy::Validator> | |
|
||
These methods are the accessor to raw values. 'raw' means the value is not decoded. | ||
|
||
=item body_parameters | ||
|
||
Accessor to decoded body parameters. It's Hash::MultiValue object. | ||
|
||
=item query_parameters | ||
|
||
Accessor to decoded query parameters. It's Hash::MultiValue object. | ||
|
||
=item json_parameters | ||
|
||
Accessor to decoded JSON body parameters. It's B<NOT> Hash::MultiValue object. | ||
|
||
post '/api' => sub { | ||
my ($self, $c) = @_; | ||
my $foo = $c->req->json_parameters->{foo}; # bar | ||
}; | ||
|
||
# requrest | ||
# $ua->requrest( | ||
# HTTP::Request->new( | ||
# "POST", | ||
# "http://example.com/api", | ||
# [ "Content-Type" => 'application/json', "Content-Length" => 13 ], | ||
# '{"foo":"bar"}' | ||
# ) | ||
# ); | ||
|
||
NOTE: Not need to set C<kossy.request.parse_json_body> to 1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When using |
||
|
||
=back | ||
|
||
=head1 Kossy::Response | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -78,6 +78,12 @@ $json_parser->register( | |||||||||||||||
'HTTP::Entity::Parser::JSON' | ||||||||||||||||
); | ||||||||||||||||
|
||||||||||||||||
my $json_only_parser = HTTP::Entity::Parser->new(); | ||||||||||||||||
$json_only_parser->register( | ||||||||||||||||
'application/json', | ||||||||||||||||
'HTTP::Entity::Parser::JSON' | ||||||||||||||||
); | ||||||||||||||||
|
||||||||||||||||
sub _build_request_body_parser { | ||||||||||||||||
my $self = shift; | ||||||||||||||||
if ( $self->env->{'kossy.request.parse_json_body'} ) { | ||||||||||||||||
|
@@ -117,11 +123,32 @@ sub uploads { | |||||||||||||||
sub body_parameters { | ||||||||||||||||
my ($self) = @_; | ||||||||||||||||
$self->env->{'kossy.request.body'} ||= do { | ||||||||||||||||
my @body_parameters = $self->env->{'kossy.request.parse_json_body'} | ||||||||||||||||
? $self->body_parameters_raw->flatten | ||||||||||||||||
: @{$self->_body_parameters()}; | ||||||||||||||||
Hash::MultiValue->new(map { _decode_recursively($_) } @{$self->_body_parameters()}); | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
sub json_parameters { | ||||||||||||||||
my ($self) = @_; | ||||||||||||||||
$self->env->{'kossy.request.json_body'} ||= do { | ||||||||||||||||
+{ map { _decode_recursively($_) } @{$self->_json_parameters()} } | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
Hash::MultiValue->new(map { Encode::decode_utf8($_) } @body_parameters); | ||||||||||||||||
sub _decode_recursively { | ||||||||||||||||
my $v = shift; | ||||||||||||||||
if (my $r = ref $v) { | ||||||||||||||||
if ($r eq 'ARRAY') { | ||||||||||||||||
return [ map { _decode_recursively($_) } @$v ]; | ||||||||||||||||
} | ||||||||||||||||
elsif ($r eq 'HASH') { | ||||||||||||||||
return { map { Encode::decode_utf8($_) => _decode_recursively($v->{$_}) } keys %$v }; | ||||||||||||||||
} | ||||||||||||||||
else { | ||||||||||||||||
die 'Cannot decode ' . $v; | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
else { | ||||||||||||||||
return Encode::decode_utf8($v); | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
|
@@ -148,6 +175,16 @@ sub _body_parameters { | |||||||||||||||
} | ||||||||||||||||
return $self->env->{'kossy.request.body_parameters'}; | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
sub _json_parameters { | ||||||||||||||||
my $self = shift; | ||||||||||||||||
unless ($self->env->{'kossy.request.json_parameters'}) { | ||||||||||||||||
my ($params) = $json_only_parser->parse($self->env); | ||||||||||||||||
$self->env->{'kossy.request.json_parameters'} = $params; | ||||||||||||||||
} | ||||||||||||||||
return $self->env->{'kossy.request.json_parameters'}; | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
sub _query_parameters { | ||||||||||||||||
my $self = shift; | ||||||||||||||||
unless ( $self->env->{'kossy.request.query_parameters'} ) { | ||||||||||||||||
|
@@ -160,9 +197,7 @@ sub _query_parameters { | |||||||||||||||
sub body_parameters_raw { | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change reverts back to the v0.60 code. Lines 155 to 161 in 4191730
|
||||||||||||||||
my $self = shift; | ||||||||||||||||
unless ($self->env->{'plack.request.body'}) { | ||||||||||||||||
$self->env->{'plack.request.body'} = $self->env->{'kossy.request.parse_json_body'} | ||||||||||||||||
? Hash::MultiValue->from_mixed(@{$self->_body_parameters}) | ||||||||||||||||
: Hash::MultiValue->new(@{$self->_body_parameters}); | ||||||||||||||||
$self->env->{'plack.request.body'} = Hash::MultiValue->new(@{$self->_body_parameters}); | ||||||||||||||||
} | ||||||||||||||||
return $self->env->{'plack.request.body'}; | ||||||||||||||||
} | ||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the reason for using Hash::MultiValue is to handle Arrays.
If JSON can handle structures such as Arrays, so I prefer to not using Hash::MultiValue.