Documentation
Sending XML
The xml
method is a useful addition to the response object, simplifying the process of sending XML responses. By setting the Content-Type
header to application/xml
and sending the provided XML content as the response body, this method ensures that XML content is correctly handled and returned to the client. This is particularly useful for API endpoints or responses where XML is the desired format, such as RSS feeds and sitemaps.
xml
Description:
The xml
method sets the Content-Type
header to application/xml
and sends the provided XML content as the response body. This method simplifies the process of returning XML responses to the client.
Signature:
xml(xmlContent: string, statusCode?: number): void
Parameters:
xmlContent
(string): The XML content to be sent as the response body.statusCode
(number, optional): The HTTP status code to be sent. Default is 200.
Example:
// Route to handle sending XML response
app.get("/xmlresponse", (req, res) => {
const xmlContent = `
<response>
<message>This is an XML response</message>
</response>
`
res.xml(xmlContent)
})
In this example, the /xmlresponse
route will send the XML response to the client.