So my endpoint is only expecting this schema:
{
A: "number",
B: "number
}
The sender sends:
{
A: "number",
B: "number,
C: "number
}
What do I do with C
?
What if the sender is my UI, which means that there is a bug in the UI.
Are there standard protocols to handle this situation?
CodePudding user response:
Some services ignore extra parameters, others reject.
A general good advice is to use something like JSON Schema to describe what bodies you are expecting, set additionalProperties
to false (better to reject unexpected data) and emit a 422
error when there was a validation error.
CodePudding user response:
If you're using a json-schema you could set the extra properties to false
{
"properties" {
A: "number",
B: "number"
}
additionalProperties = false`
}
Which causes validation and rejects extra property(s) as you describe. Or you could just deliberately ignore the extra data - if it's your UI, as you say, then perhaps you do want to investigate, but if it's someone else using your endpoint then you have no control. But setting that property will cause the json to go through some validation and handle it all for you.