3
votes

I am trying to set up a cloud function that returns an xml. I am able to create and log the xml, but it crashes with the following error when I try to return it.

TypeError: Converting circular structure to JSON at Object.stringify (native) at stringify (/var/tmp/worker/node_modules/express/lib/response.js:1119:12) at ServerResponse.json (/var/tmp/worker/node_modules/express/lib/response.js:260:14) at ServerResponse.send (/var/tmp/worker/node_modules/express/lib/response.js:158:21) at cors (/user_code/index.js:663:21) at cors (/user_code/node_modules/cors/lib/index.js:188:7) at /user_code/node_modules/cors/lib/index.js:224:17 at originCallback (/user_code/node_modules/cors/lib/index.js:214:15) at /user_code/node_modules/cors/lib/index.js:219:13 at optionsCallback (/user_code/node_modules/cors/lib/index.js:199:9)

My Function

exports.sendXMLResponeSample = functions.https.onRequest((request, response) => {
  cors(request, response, () => {
    // import xmlbuilder
    const builder = require('xmlbuilder');
    // create my object to convert to xml
    var myFeedObject = {
      "somekey": "some value",
      "age": 59,
      "eye color": "brown"
    }
    // convert myFeedObject to xml
    const feed = builder.create(myFeedObject, { encoding: 'utf-8' })
    console.log("feed.end({ pretty: true }) = (below)");
    console.log(feed.end({ pretty: true }));
    // return xml
    return response.send(200, feed) // <<< error occurs here
  })
})

I believe the error suggests the the firebase cloud function is expecting I return a JSON object in the response rather than an xml object, but I am unsure how to tell it to expect an xml object in the response.

Does anyone understand how to return an xml object in a firebase cloud function?

EDIT: The object is converted to an xml object without any issue. The error occurs when the xml object is attempted to be returned.

3
@Frank van Puffelen any idea on this?Rbar

3 Answers

1
votes

You can use the .contentType(type: string) on the response object that the cloud function returns to the caller.

Like so:

res.status(200)
   .contentType('text/xml; charset=utf8')
   .send(xmlString);
0
votes

You may install the object-to-xml library, and then set the response data type in the response header to text/XML, something like res.header('Content-type','text/xml').

0
votes

This is what I'm doing.

  const xmlString =
          '<?xml version="1.0" encoding="UTF-8"?><Response><Message><Body>This is the 
  response</Body></Message></Response>';
        res
          .set("Content-Type", "text/xml; charset=utf8")
          .status(200)
          .send(xmlString);

Works for me. I'm sure there is a better way to convert your XML to a string.