Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

263
Visualizações
How to change the json request format when input is captured via a form

We have the following form.

const MyForm = ({
  myFormRef,
}) => (
  <form id="myForm" ref={myFormRef} method="POST">
    <input name="request_token" type="hidden" />
    <input name="id" type="hidden" />
    <input name="versionId" type="hidden" />
    <input name="additionalId" type="hidden" />
  </form>
);

export default MyForm;

We are using useRef hook to reference this form and calling it within another Component as follows.

const myFormRef = useRef();

<MyForm
    myFormRef={myFormRef}
/>

The postData function at the end is in a js file.
Question is on this postData function.
This is triggered when a form is submitted via a button click.

It is all working fine where I am capturing correct details and able to submit the form.

The issue:

It is the format of how I am sending the data over which is the issue.

I am sending over details in following format in this form.

{
    token,
    id,
    versionId,
    additionalId
}

Instead I want to send it as:

{
    token,
    overallData: {
        id,
        versionId,
        additionalId
    }
}

How can I modify the request format to wrap inside overallData for the 3 fields (token is still outside) before submitting?

export const postData = (
  myFormRef,
  token,
  myLink,
  id,
  versionId,
  additionalId
) => {
  const fields = {
    request_token: token,
    id,
    versionId,
    additionalId,
  };

  // checking if data exists for a key (example id or token) and remove if no value against the key. 
  [...myFormRef.current.children].forEach((child) => {
    child.value = fields[child.name];
    if (!child.value) {
      myFormRef.current.removeChild(child);
    }
  });

  // then submit
  myFormRef.current.action = myLink;
  myFormRef.current.submit();
};

Note: I have to stick with this style to use the form and useRef() due to other dependencies.

Thus looking for a way to possibly modify postData. Else advise other options. Thanks.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Try this:

<form id="myForm" ref={myFormRef} method="POST">
    <input name="request_token" type="hidden" />
    <input name="overallData.id" type="hidden" />
    <input name="overallData.versionId" type="hidden" />
    <input name="overallData.additionalId" type="hidden" />
  </form>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda