Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

194
Vistas
how to send plain text data in fetch and get json type data in react native
fetch('http://3.108.170.236/erp/apis/login_otp.php', {
  method: 'POST',
  headers: {
    Accept: 'application/json',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    monumber: '8770276989',
  }),
})
  .then(async response => {
    console.log('fetching api');
    console.log(await response.json());
    return await response.json();
  })
  
  .catch(err => {
    console.log('fetch error' + err);
  });

postman api output

[{"otp_value":665354,"student_id":"3117"}]

getting error of 405 method not allowed. i am sending data in post format with plain text and api response is in json what should i do get proper value.

Thanking you advance community.

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Make these changes in your headers object.

  • To send plain text, set Content-Type to application/x-www-form-urlencoded.
  • To receive JSON type data set Accept to application/json. Of course, you have to be certain that the server is sending back a JSON response otherwise you will get a syntax error when parsing it on the client side.

headers object:

headers: {
    Accept: 'application/x-www-form-urlencoded',
    'Content-Type': 'application/json',
} 

If you are not sure that the response will be in JSON or plain text, you can check the response's header content-type property before acting on it:

(This is a modification of the answer here to fit your question)

...

.then(response => { 
    console.log('fetching api');

    const contentType = response.headers.get("content-type"); 

    // This is JSON type response
    if (contentType && contentType.indexOf("application/json") !== -1) {
        return response.json().then(data => {
            // The response was a JSON object
            // Process your data as a JavaScript object
        });
    } else {
        return response.text().then(text => {
            // The response wasn't a JSON object
            // Process your text as a String
        });
    }
})
// All errors can be caught here.
.catch((err) => console.log("The error:", err));

...
about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda