简体   繁体   中英

Send NodeJS ExpressJS Response HTTP Status code 200 + payload

I need to send back an acknowledge response back with the following characteristics:

  • HTTP Status code 200
  • payload (body?)

My try:

 function handleMessage(req, res){ let otaRequestBuffer = []; req.on('readable', () => { let data; while (data = req.read()) { otaRequestBuffer.push(data.toString()); } }); req.on('end', () => { let otaRequest = otaRequestBuffer.join(''); try { let parser = new DOMParser(); let xmlDoc = parser.parseFromString(otaRequest, "text/xml"); let soapHeader = parseSoapHeader(xmlDoc); const soapAction = req.headers['soapaction'].toString(); const ack = `<?xml version='1.0' encoding='utf-8'?> <env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope"> <env:Header> <htnga:CorrelationID xmlns:htnga="http://htng.org/PWSWG/2007/02/AsyncHeaders">${soapHeader.correlationId}</htnga:CorrelationID> <htnga:RelatesToCorrelationID xmlns:htnga="http://htng.org/PWSWG/2007/02/AsyncHeaders">${soapHeader.correlationId}</htnga:RelatesToCorrelationID> </env:Header> <env:Body> <ns:HTNG_AcknowledgeReceipt xmlns:ns="http://htng.org/2014B"/> </env:Body> </env:Envelope>` res.sendStatus(200); res.write(ack); } catch (error) { console.log(error); } });

But this only sends OK. How can I send also the payload/body and the HTTPS status code 200.

Thanks for the help.

Here a screenshot from the docs from the API provider:

ACK-示例

By default the status code is 200 for res.send(), but for other status codes, you can do that using

res.status(403).send("You are not authorised to view this")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM