javascriptnode.jsexpresspostmanoracle-commerce

POST call is working fine in postman but gives error in node.js


I am new to node.js and i'm trying to make a post request to an external server(Oracle Commmerce Cloud) to export some data. PFA image screenshot of the request body in Postman (content-type: application/json) Request Body In Postman

When i am trying to make the same request using express in node then it throws me error. I am not sure if there's some other way to write body raw json of postman in node requests.

Below is my node request code

let req_body = JSON.stringify({
            "fileName": fileName,
            "mode": mode,
            "id": category,
            "format": format
        })


request.post(url, {
        data :{req_body},
        headers: {
            'Content-Type': 'application/json',
            'Authorization': `Bearer ${access_token}`
        }

    }, (error, res, body) => {
        if (error) {
            console.log('An error occured while loading the page', error)
            // console.error(error)
            return
        }
       console.log("export call:", res.statusCode)
        console.log("export call:", data)
    });

I get the below console error: -

export call: 400
export call: {
errorCode: '120001',
message: 'Invalid export input data',
status: '400'
}

Kindly help me to solve this issue.


Solution

  • I'm going to assume you're using request node module, but data are passing throught body params and not data, it will look like this :

    request.post(url, {
        body: req_body,
        headers: {
            'Content-Type': 'application/json',
            'Authorization': `Bearer ${access_token}`
        }
    }, (error, res, body) => {
        if (error) {
            console.log('An error occured while loading the page', error)
            // console.error(error)
            return
        }
       console.log("export call:", res.statusCode)
        console.log("export call:", data)
    });