POST呼叫在邮递员中工作正常,但在node.js中出现错误

时间:2020-06-04 13:06:50

标签: javascript node.js express postman oracle-commerce

我是node.js的新手,我正尝试向外部服务器(Oracle Commmerce Cloud)发出发布请求以导出一些数据。 Postman中请求正文的PFA图像屏幕快照 (内容类型:application / json) Request Body In Postman

当我尝试使用node中的express发出相同的请求时,会抛出错误。我不确定是否还有其他方法可以在节点请求中编写邮递员的正文原始json。

下面是我的节点请求代码

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)
    });

我收到以下控制台错误:-

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

请帮助我解决这个问题。

1 个答案:

答案 0 :(得分:0)

我将假设您正在使用request节点模块,但是数据是通过body参数而不是data传递的,它看起来像这样:

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)
});