0

I have a basic CRUD system setup, however the system fails when special characters are used.

For example, here is my route for a delete request:

// DELETE OPERATIONS
router.route('/delete/:id').delete((req, res) => {
  const title = req.params.id;
  // console.log('DEBUG: aricles.js: server receiving title: ', title)
  DBM.deleteArticle(title).then(() => {
    console.log('DEBUG: article deleted: ', title);
  }).catch((err)=>{
    console.log('articles/delete error', err);
  });
  res.end();
});

My unique ID for an entry is a string which is the title of the article. However if the string ends with a ? which is a special character it disappears by the time it gets to the server.

The client sends the delete request as follows:

const options = { 
  headers: {'Content-Type': 'application/json'}, 
  method: 'DELETE', 
  body: JSON.stringify(this.props.data)
};

this.debug && console.log('cleint sending title: ', title);

fetch("/articles/delete/" + title, options )
  .then((response) => {
    console.log('response', response);
  })
  .catch((error) => {
    console.log('delete clicked error', error);
  });
  

I imagine other special characters in the URL would cause issues as well such as # and :, etc.

jennifer
  • 682
  • 5
  • 14
  • 1
    You should require the `title` to be encoded or use ids for that, for example, save the I’d to be used internally, and create a GUID to be used publicly – balexandre Jun 25 '20 at 03:22
  • `fetch('/articles/delete/' + encodeURIComponent(title), ...` – Phil Jun 25 '20 at 03:31

0 Answers0