Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

198
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!