I have an API endpoint that utilizes actix_web
to deserialize an incoming JSON payload (actix_web
ultimately uses serde
for JSON deserialization).
As an example, I have something that looks like this:
pub struct IncomingPayload {
pub field1: i32,
pub field2: String
}
pub async fn update_platforms(
pool: web::Data<Pool>,
req: web::Json<Vec<IncomingPayload>>,
) -> Result<HttpResponse, error::Error> {
println!(req.field1); // will be an i32
println!(req.field2); // will be a String
}
Currently, this endpoint will only successfully return if serde
is able to deserialize all fields of the struct. I.e. A request must contain field1
and field2
keys.
E.g. this would be successful:
curl --header "Content-Type: application/json" \
--request POST \
--data '{"field1": 1,"field2":"something"}' \
http://localhost:8080
But this would not (because field2
is missing from the payload):
curl --header "Content-Type: application/json" \
--request POST \
--data '{"field1": 1}' \
http://localhost:8080
So my question is, can this be done? Can one send a JSON payload with a subset of key-value fields to an actix_web
endpoint that expects all the fields to be present?
Or rather, is there a generic way to deserialize partial structs like this with actix_web
or serde
?