user2452537
user2452537

Reputation: 53

Why is my body always empty on making a post call from react js front end

React js:

export const insertUser=( name, facebookId )=> {
    return (dispatch) => {
        fetch("http://localhost:3000/insertUser", {
            method: "post",
            credentials: 'same-origin',
            mode: 'no-cors',
            redirect: 'follow',
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json',
                'Origin': '',
            },

            body: JSON.stringify({
                facebookId: facebookId,
                name: name
            }),
        }).then((response) => {
            console.log(response)
        });
    }
}

Node js express:

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.post('/insertUser, user.createUser);

The react js code sends empty body to the backend. If I make the mode: cors, I get a 404.

Upvotes: 1

Views: 129

Answers (1)

brijesh chavda
brijesh chavda

Reputation: 81

You are passing data as string and you have configured bodyparser using json.

When you are passing data as string then you should use as text like this :

app.use(bodyParser.text());

Upvotes: 1

Related Questions