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

116
Visualizações
Please help me how to get data from api with a json body in GET request

I'm implementing an autocomplete input with react-select. To load autocomplete options, I need to fetch an api. I need to do this task on two endpoints, one which is just the url (this one I got) but this one is different: I need to send a json body with the following contents : {"searcher" : "VALUE"} where the value would be the terms that the user would look for, that is, a variable that I attribute to the input. But I can't do a search and it's not returning anything when I put this code, which is basically the logic I used on the endpoint that only needs the URL. where am I going wrong?

I have the collection of the api in postman and I tested it by sending the json body and it works, but in the code, no

  const INITIAL_DATA = {
    value: 0,
    label: 'Selecione o Procedimento',
  };

  const [selectData, setselectData] = useState(INITIAL_DATA);
  const mapResponseToValuesAndLabels = (data) => ({
    value: data.id,
    label: data.terminologia,
  });

 async function callApi(value) {

 
     /* var data = JSON.stringify({
      "buscador": "CAP"
    }); */
    const api = axios.create({
      baseURL: `http://devsite.redetelemedicabr.com.br:3333/medsigtap/byTerminologia`,
      headers: { 
        'Content-Type': 'application/json',
        'Accept' : '*/*'
        
      },
      data :JSON.stringify({
        "buscador": "CAP"
      })

    })

    
      api.get('/').then(res => {
        console.log(res.data,'fefe')
      })
     
  
  }


  return (
    <div>
      <p></p>
      <AsyncSelect
        cacheOptions
        loadOptions={callApi}
        onChange={(data) => {
          setselectData(data);
        }}
        value={selectData}
        defaultOptions
      />

in this code, I need to get the response from API and map it to have a label and value so it can show it on the autocomplete options.

I'm getting on response log only an empty array [] but in postman it works , i received what I'm looking for (the CAP objs )

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

0

the problem is you are not sending the data correctly

send the jsonData like this:

const rawResponse = await fetch(url, {
  method: 'POST',
  body: JSON.stringify(jsonData),
});

const response = rawResponse.json().map(mapResponseToValuesAndLabels);
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