-
Notifications
You must be signed in to change notification settings - Fork 159
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
fix(828): ensuring valid JSON response from REST API #831
Conversation
Signed-off-by: gabrik <gabriele.baldoni@gmail.com>
1 similar comment
Signed-off-by: gabrik <gabriele.baldoni@gmail.com>
plugins/zenoh-plugin-rest/src/lib.rs
Outdated
} | ||
|
||
fn payload_to_string(payload: &Payload) -> String { | ||
String::from_utf8(payload.contiguous().to_vec()).unwrap_or(base64_encode(&payload.contiguous())) |
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.
Why not using the .deserialize()
?
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.
Or even
Line 561 in d808ba2
pub enum StringOrBase64 { |
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.
Didn't knew about it
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.
Updated code to use StringOrBase64::from
and then dereferecing to String
.
A clone()
becomes necessary but should be fine.
Signed-off-by: gabrik <gabriele.baldoni@gmail.com>
Signed-off-by: gabrik <gabriele.baldoni@gmail.com>
plugins/zenoh-plugin-rest/src/lib.rs
Outdated
} | ||
|
||
fn payload_to_json(payload: Payload, encoding: &Encoding) -> serde_json::Value { | ||
match payload.len() { |
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.
Rust prefers the usage of is_empty()
rather than checking if the len() == 0
Signed-off-by: gabrik <gabriele.baldoni@gmail.com>
This PR ensures that the response of the REST plugin is a valid JSON, Closes #828
If a sample has
application/json
ortext/json
ortext/json5
encoding then we try to convert it to aserde_json::Value
if we fail then can be converted to a string via thefrom_utf8
function then we return its payload as JSON string, otherwise it is encoded as base64 and return as JSON string.If the sample has a different encoding and can be converted to a string via the
from_utf8
function then we return its payload as JSON string , otherwise it is encoded as base64 and return as JSON string.This is an example of a get with the different cases: