【问题标题】:POST call is working fine in postman but gives error in node.jsPOST 调用在邮递员中工作正常,但在 node.js 中出现错误
【发布时间】:2020-06-04 13:06:50
【问题描述】:

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

当我尝试在节点中使用 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'
}

请帮我解决这个问题。

【问题讨论】:

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


    【解决方案1】:

    我将假设您正在使用 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)
    });
    

    【讨论】:

      猜你喜欢
      • 2020-08-07
      • 2021-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多