TT#18754 Implement json response for api-root

When requested with an "Accept: application/json" header, return
the response in json format instead of xhtml.

Change-Id: Iba4135d8013fd200af426f2eb3d67b97282c034b
changes/37/14337/3
Andreas Granig 9 years ago
parent 67f2e54be6
commit abe78df430

@ -208,13 +208,31 @@ sub GET : Allow {
} else {
$c->stash(is_admin_api => 1);
}
$c->stash(template => 'api/root.tt');
$c->forward($c->view);
$c->response->headers(HTTP::Headers->new(
Content_Language => 'en',
Content_Type => 'application/xhtml+xml',
#$self->collections_link_headers,
));
if($c->req->header('Accept') && $c->req->header('Accept') eq 'application/json') {
my $body = {};
foreach my $rel(keys %{ $c->stash->{collections} }) {
my $r = $c->stash->{collections}->{$rel};
foreach my $k(qw/
actions item_actions fields query_params sorting_cols
/) {
$body->{$rel}->{$k} = $r->{$k};
}
}
$c->response->body(JSON::to_json($body, { pretty => 1 }));
$c->response->headers(HTTP::Headers->new(
Content_Language => 'en',
Content_Type => 'application/json',
));
} else {
$c->stash(template => 'api/root.tt');
$c->forward($c->view);
$c->response->headers(HTTP::Headers->new(
Content_Language => 'en',
Content_Type => 'application/xhtml+xml',
#$self->collections_link_headers,
));
}
return;
}

Loading…
Cancel
Save