]> git.proxmox.com Git - pve-http-server.git/commitdiff
fix #2766: allow application/json as content-type for post/put requests
authorDominik Csapak <d.csapak@proxmox.com>
Fri, 29 May 2020 13:29:00 +0000 (15:29 +0200)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Fri, 5 Jun 2020 07:07:29 +0000 (09:07 +0200)
this makes creating an api client much nicer

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
PVE/APIServer/AnyEvent.pm

index 3d755d4392f76c077631cb879dbaaa742015d02d..e5f2cb4d200dff06bd2d6ee925f0f488cc6b039f 100644 (file)
@@ -44,6 +44,7 @@ use HTTP::Headers;
 use HTTP::Request;
 use HTTP::Response;
 use Data::Dumper;
+use JSON;
 
 my $limit_max_headers = 30;
 my $limit_max_header_size = 8*1024;
@@ -693,7 +694,15 @@ sub extract_params {
     my $params = {};
 
     if ($method eq 'PUT' || $method eq 'POST') {
-       $params = decode_urlencoded($r->content);
+       my $ct;
+       if (my $ctype = $r->header('Content-Type')) {
+           $ct = parse_content_type($ctype);
+       }
+       if (defined($ct) && $ct eq 'application/json')  {
+           $params = decode_json($r->content);
+       } else {
+           $params = decode_urlencoded($r->content);
+       }
     }
 
     my $query_params = decode_urlencoded($r->url->query());
@@ -1356,7 +1365,7 @@ sub unshift_read_header {
                        return;
                    }
 
-                   if (!$ct || $ct eq 'application/x-www-form-urlencoded') {
+                   if (!$ct || $ct eq 'application/x-www-form-urlencoded' || $ct eq 'application/json') {
                        $reqstate->{hdl}->unshift_read(chunk => $len, sub {
                            my ($hdl, $data) = @_;
                            $r->content($data);