What are the steps to verify a query's accuracy and effectively manage any errors that may arise

When using my API, the endpoint '/api/students' will display all student names. If a req.query is input with a specific name, such as "John", the endpoint becomes '/api/students?name=John'. I have implemented regex to ensure that only characters and spaces are allowed in the input:

const re = /^[a-zA-Z ]*$/;
if (!re.test(name)) {//error code

However, if a user accidentally inputs the endpoint as '/api/students?firstnames=John', they will still receive all names instead of triggering an error handling response as a bad request. How can this situation be addressed?

Answer №1

If I grasp your issue correctly, it seems to revolve around distinguishing between the query parameter name and firstnames. It might be beneficial to limit user input choices for query parameters (e.g., name,

age</code) through UI controls known as "filters."</p>

<p>It appears that the API is disregarding the unfamiliar query parameter <code>firstnames
and returning all student data instead. To address this, consider handling unexpected query parameters in the API endpoint implementation by returning a 400 BAD REQUEST error when receiving an unknown parameter like firstnames.

To verify these parameters, employing regex checks seems like a promising approach. Whether implementing validation on the UI or API side, these strategies can help regulate query parameter types:

UI Approach:

  1. Restrict direct user entry of query parameter types
  2. Prompt users to input specific query parameters like name
  3. Construct the query string with user-inputted parameters

API Approach:

  1. Validate query parameters to ensure they are recognized
  2. Return a 400 BAD REQUEST response for any unrecognized parameters rather than ignoring them

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The object tag does not function properly for video fallbacks when using the appendChild method

My video player application utilizes a modified version of video For everybody. The setup involves an HTML5 <video> tag encompassing an <object> tag for flash fallback on Internet Explorer. Everything functions smoothly when I employ this setup ...

Multer does not generate a new file

I am encountering an issue related to file creation using multer. File structure: My application folder Back config -config.json images middleware -auth.js -multer-config.js migrations -create-use ...

The validation module express-validator featured in the MDN tutorial for Express

The tutorial on MDN explains how to implement validation using the following code snippet // Ensure that the name field is not empty. body('name', 'Genre name required').isLength({ min: 1 }).trim(), I'm confused about why the tri ...

After Effects Script: Apply various character styles from main text to subtext

I'm currently working on an After Effects script that can transfer the style and text of a text layer to another text layer in a different comp. The script works perfectly if the parent text layer only has one style, but I need it to handle a situatio ...

Using jQuery to send arrays to PHP with $.getJSON

My script includes javascript code to pass an array to PHP like so: var mapIDArray = ["4f8d7684791635ec2e000000", "4f8cbc087916359181000000"]; $.getJSON("rebound.php", { 'mapIDs[]' : mapIDArray ...

Zero results returned for the angularjs script

I am working on enhancing my skills in angularjs, but I am facing an issue where only the categories are being displayed and the products are not showing up. There are no error messages, so I am having trouble pinpointing where the problem lies. Here is t ...

Adding the Load More feature to your WordPress template for displaying custom posts

On my website, I am retrieving posts of a custom post type using get posts. However, I am retrieving all posts at once, but in my template, I want to display x number of posts per page and load the remaining posts using a load more button. <?php ...

The && operator is not executed when the button is disabled

There is a button on the page that has been disabled using the [disabled] property. <button class="btn btn-primary center-block" (click)="sign(sf.value)" [disabled]="validateInsuredFirstName(firstName.value) && validateInsuredLastName(l ...

Dealing with CORS error when making requests to Firebase Realtime Database API within a Vue project

I am currently working with vue-2 and I need to perform a shallow query on the Firebase real-time database by fetching an API. However, when running on my development server, I encounter a CORS blocked issue. What steps should I take to resolve this? PS: I ...

Using Ajax to implement the Modal IF functionality

Currently in the process of registering a domain, utilizing two modals in the process. Through Ajax, I have set it up so that if the response is 1, an alert stating that the domain is available is displayed. If the response is 0, an alert stating that the ...

Encountering an error when attempting to parse a JSON Object in Java from an AJAX call

** Latest Code Update ** My JavaScript and Ajax Implementation: $(function() { $("#create_obd").bind("click", function(event) { var soNumber = []; $('#sales_order_lineItems input[type=checkbox]:checked').eac ...

JavaScript for Office Spreadsheet Titles

I'm having trouble fetching the names of sheets from an external Excel file, as I keep getting an empty array. async function retrieveSheetNames() { const fileInput = <HTMLInputElement>document.getElementById("file"); const fileReader ...

Step-by-Step Guide on Automatically Uploading a File From a Link

When it comes to uploading a file, I can achieve this using the following code : <!DOCTYPE html> <html> <head> <title>File Upload</title> <link rel="stylesheet" type="text/css" href="main.css" ...

Creating a feature to display a set amount of div elements at first, then revealing additional elements:

Creating a review system using HTML, PHP, and AJAX allows users to share their opinions on a website. The system is made up of two main scripts: PHP Script: <?php ... code ?> <div class="overall_rating"> <span class="num"><?=num ...

Creating eye-catching images on your map with React Mapbox GL: Using Layer and Feature instead of Markers

For my project, I have integrated React Mapbox GL and encountered performance issues when using Markers for a large number of data points. After researching the documentation, I discovered a helpful tip: Note: When handling numerous objects, it is recom ...

"Guidelines for inserting an image from user input into a HTML div

I am trying to dynamically add input file images to separate divs on an HTML page without using get element by class name. Each div should have a unique image associated with it instead of adding the same image to all divs with the same class. Can anyone p ...

A guide to updating nested properties within MongoDB with the help of Express

In my MongoDB database, I have stored information about the movie "Amazing Spider Man 2": { "title": "Amazing Spider Man 2", "description": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry&ap ...

Seeking to develop a pair of functions for submitting data via my website's form

I need to pass form data to both Docusign and Zapier with just one button click using JavaScript. When the Submit button is pressed, I want the data to be sent to Zapier without opening a success page and then redirect to the Docusign page with all the in ...

Establish accuracy on additional inputs through directives when button is clicked within AngularJS

I lack familiarity with AngularJS directives because I typically rely on controllers. Can directives be used to set validity on other inputs? Specifically, I am trying to set validity on a certain input text when a button is clicked, but I can't figur ...

Utilize the angular ui-bootstrap datepicker exclusively for inputting dates in your application

Is there a way to disable key input and require users to use the datepicker to add dates to the input field? Currently, users can both select a date from the datepicker and manually type or erase the date in the input field. I would like to disable manual ...