Search code examples
javascriptnode.jsrequestresponsehttp-status-codes

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-Example


Solution

  • 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")