Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When parsing JSON in FormData, creates an exception #12

Open
TiLopes opened this issue Feb 22, 2023 · 6 comments
Open

When parsing JSON in FormData, creates an exception #12

TiLopes opened this issue Feb 22, 2023 · 6 comments

Comments

@TiLopes
Copy link

TiLopes commented Feb 22, 2023

{autor:asd,dataLimite:2023-02-21,dataOcorrencia:2023-02-21,descricao:asdas,infoAdicional:sadads,origem:asd,titulo:asdsa}
 ^

SyntaxError: Unexpected token a in JSON at position 1
    at JSON.parse (<anonymous>)
    at createOcorrencia (D:\Repos\PAP\backendv2\controllers\ocorrencias\controller.js:57:19)
    at Layer.handle [as handle_request] (D:\Repos\PAP\backendv2\node_modules\.pnpm\[email protected]\node_modules\express\lib\router\layer.js:95:5)
    at next (D:\Repos\PAP\backendv2\node_modules\.pnpm\[email protected]\node_modules\express\lib\router\route.js:144:13)
    at verifyPermission (D:\Repos\PAP\backendv2\middleware\verifyPermissions.js:47:5)      
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

If I remove the middleware it starts working.

@pariazar
Copy link
Owner

Hi you have a syntax error inside of your code,
you need to use it as middleware like following code:

const sanitizer = require("perfect-express-sanitizer");
 
 app.use(sanitizer.clean({
     xss: true,
     noSql: true,
     sql: true
 }));

@TiLopes
Copy link
Author

TiLopes commented Feb 22, 2023

I have it just like that.

@TiLopes
Copy link
Author

TiLopes commented Feb 22, 2023

This is how my FormData is composed. First part json and then all the files to be sent.

 let formData = new FormData();
    const json = JSON.stringify({
      autor: data.autor,
      dataLimite: data.dataLimite.toISOString().slice(0, 10),
      dataOcorrencia: data.dataOcorrencia.toISOString().slice(0, 10),
      descricao: data.descricao,
      infoAdicional: data.infoAdicional,
      origem: data.origem,
      titulo: data.titulo,
    });

    formData.append("document", json);

    if (acceptedFiles.length > 0) {
      acceptedFiles.forEach((f) => {
        formData.append("files[]", f, f.name);
      });
    }

This is how I get the JSON information in the server

  const {
    autor,
    dataLimite,
    dataOcorrencia,
    descricao,
    infoAdicional,
    origem,
    titulo,
  } = JSON.parse(req.body.document);

@amansatija
Copy link

hey any solution to this. ??

@amansatija
Copy link

on further investigation i have found out that if you keep sql:false then it transmits multipart form data how ever when u keep sql as true then in that case it does transmit .. so the bug has got something to do with the sql module

@jvfrodrigues
Copy link

jvfrodrigues commented Aug 7, 2024

It seems to have to do also with having it on verification of headers normally formdata content type looks like this
multipart/form-data; boundary=---------------------------35408058081073067362132698594

Having the middleware makes it look like this
multipart/form-data boundary=35408058081073067362132698594

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants