AI • Science • Technology • Future

Side Projects

Cleaning Input Before It Reaches the API

Image from the original article
Featured image: original publisher image. Via original publisher.

Users don’t paste links the way developers expect. On ramrom.io, the link input box regularly receives messy pastes—lines broken mid-link, trailing or leading spaces, tabs, or invisible characters. These formatting quirks are common when users copy links from messaging apps like WhatsApp or Messenger, where message formatting is not designed with web input boxes in mind. Source: dev.to.

What Happened on the Site

Initially, the site’s error logs revealed a recurring pattern: links were being rejected as invalid, even when they were correct. The error messages stated that the URL was malformed. Developer Mobeen Ahmed Gondal initially interpreted these as real failures in the system. He tested one of the failing links directly in his terminal and observed the actual issue: a newline character had been pasted directly into the middle of a video ID. The link itself was valid. The problem was not with the content, but with how it was formatted. The API rejected it because it encountered a line break where a video ID should have been.

This wasn’t a user error—it was a system flaw. The code assumed that any string pasted into the input box would be clean and properly structured. In reality, users often paste content with embedded line breaks, invisible characters, and extra spacing that developers rarely encounter during testing. The real world doesn’t follow idealized assumptions about input.

How the Cleaning Process Works

Now, every pasted link on ramrom.io undergoes a strict cleaning process before any validation occurs. This process is minimal, efficient, and designed to be foolproof. It runs on the server side in PHP, ensuring that no client-side manipulation can bypass it.

  1. Trim the entire string: Removes leading and trailing whitespace—spaces, tabs, newlines. This ensures the string starts and ends cleanly.
  2. Strip all internal whitespace: Removes spaces, tabs, carriage returns, and newlines inside the string. A video link never legitimately contains a space, so this doesn’t break a real link. This step collapses multi-line pastes into a single, continuous URL.
  3. Remove invisible characters: Includes zero-width spaces, which appear blank but still break URLs. These characters are often introduced by mobile messaging apps and are invisible to the human eye but disruptive to parsing logic.

Only after this cleaning does the system verify whether the result is a valid URL and whether it belongs to a supported platform. This sequence is critical: validation must occur after cleaning, never before.

Image from the original article
original publisher image. Via original publisher. · Source

Why the Order Matters

If validation happens before cleaning, links that were valid under messy formatting will be rejected. This results in an error message that is technically accurate but completely unhelpful. The user sees an error for a link they know is correct. Such errors are especially frustrating for users who paste from WhatsApp or Messenger—apps where line breaks and invisible characters are common. The system must not assume that a malformed appearance means a malformed content.

What the Developer Does Not Do

Mobeen deliberately avoids attempting to reconstruct or guess what a user intended. If the cleaning process produces a non-URL or a link from an unsupported platform, the system stops and communicates this clearly. Reconstructing a broken link from fragments could result in the wrong video being delivered. The user would only discover the error when the incorrect file appears on their phone. Instead of offering a speculative answer, the system provides an honest refusal. This approach prioritizes correctness over confidence.

Client-Side vs. Server-Side Cleaning

The cleaning process runs on both the client and server sides. On the client, it immediately displays the cleaned version in the input box. This gives users instant feedback and helps them see that their input has been properly processed. However, this client-side version is purely cosmetic. The server is the only place where the code can be trusted. A browser could be manipulated, bypassed, or spoofed by a script that never loaded the page. Therefore, server-side cleaning remains the only reliable safeguard.

Why This Matters

Fixing input is kinder than explaining errors. Users don’t need to understand URL encoding to use a service. For a small set of string functions—no API calls, no database, no extra round trip—this change turned a class of errors into successful downloads. It demonstrates that the boundary between code and real-world use is the input box. Every assumption about user input is wrong in some small way. The solution is consistent: clean first, validate second, and never blame the user. For developers building user-facing tools, this is a simple but powerful lesson. The real world doesn’t follow code assumptions.

Learn more about how messages travel from send to read in this article. Explore how QR codes can act as APIs in this piece.

Sources & further reading

Featured image: original publisher image. Via original publisher. Image source