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

173
Views
JSON.stringify() not working, but console.log() returns JSON

So I am making a request to an API endpoint. When I print JSONBody which is the variable data passed to POSTRequest(), I get the JSON I created printed to the log, however when I then try and JSON.Stringify() it, I get returned an empty object?

Was just wondering what I am doing wrong and how to fix it :)

getFileData()

const getFileData = () => {
    var data = {}

    // DO SOME STUFF HERE TO data

    POSTRequest(data);
}

POSTRequest()

const POSTRequest = async (JSONBody) => {
    console.log(JSONBody)
    console.log(JSON.stringify(JSONBody))
    try {
        const response = await fetch(API_ENDPOINT, {
            method: 'POST',
            body: JSON.stringify(JSONBody),
            headers: { 'Content-Type': 'application/json' }
        });
        response.json()
            .then(function (res) {
                Promise.resolve(res)
                    .then(function (finalData) {
                        console.log(finalData);
                        props.setData(finalData);
                    });
            });
    } catch (error) {
        console.log(error);
    }
} 
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You're not waiting for the response from the fetch call correctly, try this:

const POSTRequest = async (JSONBody) => {
    console.log(JSONBody)
    console.log(JSON.stringify(JSONBody))

        await fetch(API_ENDPOINT, {
            method: 'POST',
            body: JSON.stringify(JSONBody),
            headers: { 'Content-Type': 'application/json' }
        }).then((response) => {
           console.log(response);
           props.setData(response);
       }).catch((error) => {
          console.log('POSTRequest error: ' + error)
       })
   });
about 4 years ago · Juan Pablo Isaza 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!