6

Let’s say there is a REST API which allows users to create objects. These objects have a name. The name doesn’t need to be unique among all objects and doesn’t have any special meaning within the API. Should the API strip whitespace before creating the object?

Pro stripping

  • Most of the time whitespace (at least leading and trailing) is unintentional
  • It’s not visible anyway on a web interface (unless explicitly displayed as nbsp)

Contra stripping

  • It’s wrong to modify user data
  • An API is a programming interface, expect input to really be what the user wants
asked Dec 3, 2016 at 13:16
2
  • 1
    I guess you can't get a useful answer without telling the full requirements. So: it depends. Commented Dec 3, 2016 at 13:46
  • Not enough information. It really depends on how important the whitespace is to the users. Some APIs might be ok to do this, some shouldn't do this. Commented Dec 3, 2016 at 14:42

1 Answer 1

11

Either the API should accept the whitespace as a legal part of the name, or it should reject it with an exception. Silently modifying input data is a bad idea in an API. Say the client wants to retrieve the object again by searching on the name. Or the client creates multiple objects, differing only in whitespace. Any number of things could go wrong in a way that will be surprising and possibly very hard to diagnose for the client.

It makes sense to sanitize end-user input to an UI, but this is an API, not an UI intended for end-users.

answered Dec 3, 2016 at 14:15

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.