public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Dominik Csapak <d.csapak@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH proxmox] rest-server: accept empty body as valid parameters
Date: Thu, 24 Aug 2023 12:22:31 +0200	[thread overview]
Message-ID: <20230824102231.4085335-1-d.csapak@proxmox.com> (raw)

technically an empty string is not valid json, but when sending an api
request without any parameters, treating the empty body as an empty
parameter hash instead of an error, makes the the api more robust for
clients

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
 proxmox-rest-server/src/rest.rs | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/proxmox-rest-server/src/rest.rs b/proxmox-rest-server/src/rest.rs
index 0ee9fab..2ccd4d5 100644
--- a/proxmox-rest-server/src/rest.rs
+++ b/proxmox-rest-server/src/rest.rs
@@ -349,7 +349,12 @@ async fn get_request_parameters<S: 'static + BuildHasher + Send>(
         std::str::from_utf8(&body).map_err(|err| format_err!("Request body not uft8: {}", err))?;
 
     if is_json {
-        let mut params: Value = serde_json::from_str(utf8_data)?;
+        // treat empty body as empty paramater hash
+        let mut params: Value = if utf8_data.is_empty() {
+            Value::Object(serde_json::Map::new())
+        } else {
+            serde_json::from_str(utf8_data)?
+        };
         for (k, v) in uri_param {
             if let Some((_optional, prop_schema)) = param_schema.lookup(&k) {
                 params[&k] = prop_schema.parse_simple_value(&v)?;
-- 
2.30.2





             reply	other threads:[~2023-08-24 10:23 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-24 10:22 Dominik Csapak [this message]
2023-09-07 16:15 ` [pbs-devel] applied: " Thomas Lamprecht

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230824102231.4085335-1-d.csapak@proxmox.com \
    --to=d.csapak@proxmox.com \
    --cc=pbs-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal