如何从API检索返回应用程序/八位字节流类型数据的JSON对象?

时间:2019-06-12 11:37:38

标签: javascript json express request

我有一个Express应用,该应用已配置为另一个3rd party应用的webhook。 Webhook中的端点之一接收来自第三方应用程序的响应。响应内容为application/octet-stream类型。

消息的标题内容如下-

{
  "host": "event-simulator.cfapps.sap.hana.ondemand.com",
  "user-agent": "Vert.x-WebClient/3.6.2",
  "content-length": "335",
  "b3": "...",
  "content-type": "application/octet-stream",
   ....
}

现在,在快速端点中,我尝试使用JSON.stringify(body)打印正文。但它会打印一个空的正文,即{}

代码-

const express = require('express');

const app = express();
const port = process.env.PORT || 3000;
app.use(express.json());

app.get('/', (req,res)=>{
    res.send('Default Page')
})

app.post('/events', (req,res)=>{

    body = req.body
    console.log("Headers - "+JSON.stringify(req.headers));
    console.log('Message - ' + JSON.stringify(body)

    if( body.length > 2 ){
        return res.status(200).send('Successful')
    }
    return res.status(400).send('Error: Missing event payload')
})


app.listen(port, () => {
    console.log('server started on port ' + port);
})

但是,即使响应类型仍然为application/octet-stream,从PostMan调用相同的API仍能正常工作

邮递员中的样本回复-

{
    "eventType": "BChanged",
    "eventTime": "2019-06-12T10:26:13Z",
    "contentType": "application/json",
    "data": {
        "KEY": [
            {
                "PARTNER": "1234555"
            }
        ]
    }
}

标题-

Content-Length →335
Content-Type →application/octet-stream
Date →Wed, 12 Jun 2019 10:26:24 GMT
Strict-Transport-Security →max-age=31536000; includeSubDomains; preload;

0 个答案:

没有答案