# Can a canister return generated xml?

**URL:** <https://forum.dfinity.org/t/can-a-canister-return-generated-xml/1636>\
**Category:** Developers\
**Created:** [December 8, 2020, 5:19pm UTC](https://forum.dfinity.org/t/can-a-canister-return-generated-xml/1636 "2020-12-08T17:19:40Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![mymikemiller](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/mymikemiller/32/694_2.png) [@mymikemiller](https://forum.dfinity.org/u/mymikemiller)\
**Post date:** [December 8, 2020, 5:19pm UTC](https://forum.dfinity.org/t/can-a-canister-return-generated-xml/1636/1 "2020-12-08T17:19:40Z")

</div>

For my project, a host for rss feeds for podcasts, an xml file needs to be returned containing links to media determined from the data in the canister.

But I can’t figure out how to get a canister to return generated xml that a podcast player can parse.

A normal canister won’t work because it only returns data in Candid format, which podcast readers can’t parse.

An asset canister won’t work because, as far as I know, it must go through webpack or can only return static assets. I need to generate the xml on the fly based on the data in the canister and the values in the url. I tried using Mithril for simple routing/url parsing, but because it still goes through webpack I can’t serve just the generated xml. It’s always polluted by the single-page-application scaffolding that webpack forces upon me.

Is it possible to get _just_ a generated xml feed out of the IC?

---

<div class="post-metadata">

**Author:** ![cryptoschindler](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/cryptoschindler/32/26_2.png) [@cryptoschindler](https://forum.dfinity.org/u/cryptoschindler)\
**Post date:** [December 9, 2020, 11:51am UTC](https://forum.dfinity.org/t/can-a-canister-return-generated-xml/1636/2 "2020-12-09T11:51:40Z")

</div>

Maybe I’m misunderstanding your question, but can’t you just create a query function in a canister that creates your XML document and returns it as a string? You could then call that function from your frontend JS to further process (parse) it.

---

<div class="post-metadata">

**Author:** ![mymikemiller](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/mymikemiller/32/694_2.png) [@mymikemiller](https://forum.dfinity.org/u/mymikemiller)\
**Post date:** [December 9, 2020, 7:23pm UTC](https://forum.dfinity.org/t/can-a-canister-return-generated-xml/1636/4 "2020-12-09T19:23:27Z")

</div>

Those were my thoughts exactly, but I ran into a couple problems with that solution.

Say I have the following query function:

```auto
public query func getXml(): async Text {
    return "<?xml version='1.0'?><rss xmlns:itunes=...";
};

```

Instead of getting back just the xml, I get back the candid string including the parenthesis and double-quote:

```auto
dfx canister call myCanister getXml
("<?xml version='1.0'?><rss xmlns:itunes=...")

```

That’s not going to be parsed successfully by all podcast players. The xml needs to start with `<?xml` not `("<?xml`.

---

<div class="post-metadata">

**Author:** ![mymikemiller](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/mymikemiller/32/694_2.png) [@mymikemiller](https://forum.dfinity.org/u/mymikemiller)\
**Post date:** [December 9, 2020, 7:25pm UTC](https://forum.dfinity.org/t/can-a-canister-return-generated-xml/1636/5 "2020-12-09T19:25:03Z")

</div>

Even if it did, I don’t know how to get the results of that function just by fetching a url in a browser or curl command. What’s the syntax for calling a canister function by specifying the function name and parameters in the url, similar to how you can use `dfx canister call` in the terminal? Is that even possible?

I was hoping I could discover the url format by loading up `http://127.0.0.1:8000/candid?canisterId=` for my canister and pressing the `Query` button under `getXml()`, but that sends off a `POST` request to `http://127.0.0.1:8000/api/v1/read` with an unintelligible payload:

```auto
ÙÙ÷£gcontent¦cargFDIDLkcanister_idRningress_expiryeO!¨¬íkmethod_namefgetXmllrequest_typeequeryfsenderXí·ï@¥îÊç0¶ÆäêKÉðmsender_pubkeyX óæ#
íü8®ª@"çÇ­å´ÞÀÚb¥M¤jsender_sigX@¹¹ä´µeÎóyoRä\9±ÆÇ'¨y9½D>ùëÈp|ggàóK\\]´0íÓ<N(Á

```

Surely that’s not how I’m supposed to interact with my canister via curl. Is it possible to do so?

I haven’t been able to get the latter idea to work either, using frontend JS to parse and return just the xml. This may be because I’m inexperienced with webpack, but my understand is that the entire SPA gets bundled up into a single JS file. Routing inside the SPA, through React or Mithril or whatever, can do everything I need in terms of parsing the url and preparing the correct xml to return, but it can’t return just the generated xml file. It can modify the DOM and insert the xml content into the body of the page, but that’s not what I need. Again, I have little to no experience with webpack, but afaik it’s impossible to get it to respond to a request with just plain xml, unless that xml is a static file declared in webpack.config.js, which of course it isn’t.

---

<div class="post-metadata">

**Author:** ![cryptoschindler](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/cryptoschindler/32/26_2.png) [@cryptoschindler](https://forum.dfinity.org/u/cryptoschindler)\
**Post date:** [December 10, 2020, 8:45am UTC](https://forum.dfinity.org/t/can-a-canister-return-generated-xml/1636/6 "2020-12-10T08:45:00Z")

</div>

But here you are calling from the command line, I believe calling that same function from within your frontend will return just the string. Even if not, you should be able to process the string to get rid of those ”candid extras”, right?

But from what you are saying it seems that it’s not that trivial and I am in no way an experienced frontend developer 🙂

---

<div class="post-metadata">

**Author:** ![mymikemiller](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/mymikemiller/32/694_2.png) [@mymikemiller](https://forum.dfinity.org/u/mymikemiller)\
**Post date:** [December 10, 2020, 4:01pm UTC](https://forum.dfinity.org/t/can-a-canister-return-generated-xml/1636/7 "2020-12-10T16:01:55Z")

</div>

I’m not sure how to return “just the string” from frontend code that goes through webpack. I can access the string and process it, but then what? How do I return a page that consists of only that xml string, with no surrounding html? Maybe there’s a way; I’m new to webpack too.

---

<div class="post-metadata">

**Author:** ![PaulLiu](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/paulliu/32/4701_2.png) [@PaulLiu](https://forum.dfinity.org/u/PaulLiu)\
**Post date:** [January 6, 2021, 4:44am UTC](https://forum.dfinity.org/t/can-a-canister-return-generated-xml/1636/8 "2021-01-06T04:44:56Z")

</div>

At the moment, a canister cannot serve plain HTML or XML response. If you really want a plain text to be returned from a canister, you can try [the proxy solution](https://forum.dfinity.org/t/can-we-somehow-achieve-or-build-a-canister-with-a-interface-bahaving-like-a-classic-web-server/1257/13) given by @nometa in another discussion thread. _EDIT: just realized that thread was also a reply to your questions 🙂_

We are aware that serving plain HTML (or data encoded in other HTTP content type) directly from the canister end point is a popular demand. But the JSON format at the moment contains extra data besides the return value of calling a method, e.g., signature and proof that can help verify the authenticity of the return value.

---

<div class="post-metadata">

**Author:** ![mymikemiller](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/mymikemiller/32/694_2.png) [@mymikemiller](https://forum.dfinity.org/u/mymikemiller)\
**Post date:** [June 25, 2021, 10:48pm UTC](https://forum.dfinity.org/t/can-a-canister-return-generated-xml/1636/9 "2021-06-25T22:48:29Z")

</div>

@anon74414410 Do the [recently improved asset canisters](https://www.notion.so/Announcing-Improved-Asset-Canisters-7b5815f6ca46461dae4b7f22501f4e39) make it possible to return generated XML now?

I think that, with the dependency on Webpack removed, I should be able to return a page that consists of only my generated xml string, with no surrounding html like webpack was surrounding my page with.

I can put a static rss file under my\_canister\_assets/assets and access it via url. That works fine and I get back just my xml as expected. But if I want to generate the xml file based on information in the canister, I need to use javascript and because of that, I still end up with some html surrounding the xml I intend to return. For example, if index.html is set to run a javascript file on load like so:

(I keep getting 403 errors trying to post this, so I’m splitting it up into multiple posts. Sorry.)

---

<div class="post-metadata">

**Author:** ![mymikemiller](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/mymikemiller/32/694_2.png) [@mymikemiller](https://forum.dfinity.org/u/mymikemiller)\
**Post date:** [June 25, 2021, 10:53pm UTC](https://forum.dfinity.org/t/can-a-canister-return-generated-xml/1636/10 "2021-06-25T22:53:01Z")

</div>

I couldn’t post the html here, even in a triple backtick section. Here’s the code in pastebins:

my\_canister\_assets/src/index.html  
[https://pastebin.com/raw/453VRQLs](https://pastebin.com/raw/453VRQLs)

my\_canister\_assets/src/index.js:  
[https://pastebin.com/raw/VNzGQnks](https://pastebin.com/raw/VNzGQnks)

---

<div class="post-metadata">

**Author:** ![mymikemiller](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/mymikemiller/32/694_2.png) [@mymikemiller](https://forum.dfinity.org/u/mymikemiller)\
**Post date:** [June 25, 2021, 10:53pm UTC](https://forum.dfinity.org/t/can-a-canister-return-generated-xml/1636/11 "2021-06-25T22:53:38Z")

</div>

When I deploy and navigate to the page, I get back the following, with the rss tag buried under the unnecessary and bothersome html/body tags.

 ![Screen Shot 2021-06-25 at 4.09.32 PM](https://us1.discourse-cdn.com/flex023/uploads/dfn/original/2X/c/c10deefd8f8dd69a6f0d78b7c996a1648d936539.png)

Is it possible yet to have a canister return pure generated xml without going through a solution hosted on aws or somewhere else like @nomeata’s ic\_http\_bridge? @paulyoung mentioned implementing http\_request in [this post](https://forum.dfinity.org/t/is-it-possible-to-build-a-rest-api-in-a-canister/5355/2), but I can’t find any hints on how to do that in motoko. I realize there’s an ongoing effort to support update calls in this way; I’m fine with query calls for now.

Thanks, and sorry for the three-post question!

---

<div class="post-metadata">

**Author:** ![paulyoung](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/paulyoung/32/5251_2.png) [@paulyoung](https://forum.dfinity.org/u/paulyoung)\
**Post date:** [June 25, 2021, 11:33pm UTC](https://forum.dfinity.org/t/can-a-canister-return-generated-xml/1636/12 "2021-06-25T23:33:20Z")

</div>

You can. @lastmjs does this here: [https://ic3o3-qiaaa-aaaae-qaaia-cai.raw.ic0.app/](https://ic3o3-qiaaa-aaaae-qaaia-cai.raw.ic0.app/)

Here’s an example of implementing `http_request` in Motoko that I wrote: [feat: infer update call based on HTTP request method by paulyoung · Pull Request #195 · dfinity/agent-rs · GitHub](https://github.com/dfinity/agent-rs/pull/195)

---

<div class="post-metadata">

**Author:** ![paulyoung](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/paulyoung/32/5251_2.png) [@paulyoung](https://forum.dfinity.org/u/paulyoung)\
**Post date:** [June 25, 2021, 11:41pm UTC](https://forum.dfinity.org/t/can-a-canister-return-generated-xml/1636/13 "2021-06-25T23:41:18Z")

</div>

Actually, I’m not sure if @lastmjs serves the RSS feed from the IC but it’s definitely possible.

I’ve considered it as a way to provide notifications for users but had some concerns around the costs of cycles from the constant polling.

---

<div class="post-metadata">

**Author:** ![paulyoung](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/paulyoung/32/5251_2.png) [@paulyoung](https://forum.dfinity.org/u/paulyoung)\
**Post date:** [June 25, 2021, 11:52pm UTC](https://forum.dfinity.org/t/can-a-canister-return-generated-xml/1636/14 "2021-06-25T23:52:12Z")

</div>

To be totally clear, I’ve only done this via my forked version of `icx-proxy` running locally.

I have reason to believe this works when making a request directly to the replica/network without a local proxy but for certain reasons I haven’t actually done that yet.

---

<div class="post-metadata">

**Author:** ![lastmjs](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/lastmjs/32/3406_2.png) [@lastmjs](https://forum.dfinity.org/u/lastmjs)\
**Post date:** [June 26, 2021, 12:24am UTC](https://forum.dfinity.org/t/can-a-canister-return-generated-xml/1636/15 "2021-06-26T00:24:41Z")

</div>

I definitely do host the RSS xml file from the IC, it’s awesome and is working great so far

---

<div class="post-metadata">

**Author:** ![paulyoung](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/paulyoung/32/5251_2.png) [@paulyoung](https://forum.dfinity.org/u/paulyoung)\
**Post date:** [June 26, 2021, 12:46am UTC](https://forum.dfinity.org/t/can-a-canister-return-generated-xml/1636/16 "2021-06-26T00:46:16Z")

</div>

I also just tried making a request directly to the local replica and it worked as expected, although it’s still not clear to me if this is due to `icx-proxy` being run locally in the background on my behalf and if it works exactly the same way when deployed to the IC.

---

<div class="post-metadata">

**Author:** ![anon74414410](https://avatars.discourse-cdn.com/v4/letter/a/85f322/32.png) [@anon74414410](https://forum.dfinity.org/u/anon74414410)\
**Post date:** [June 26, 2021, 12:52am UTC](https://forum.dfinity.org/t/can-a-canister-return-generated-xml/1636/17 "2021-06-26T00:52:23Z")

</div>

Yeah, you can totally serve an RSS feed from an asset canister. Not a big deal at all

---

<div class="post-metadata">

**Author:** ![mymikemiller](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/mymikemiller/32/694_2.png) [@mymikemiller](https://forum.dfinity.org/u/mymikemiller)\
**Post date:** [July 6, 2021, 11:46pm UTC](https://forum.dfinity.org/t/can-a-canister-return-generated-xml/1636/18 "2021-07-06T23:46:31Z")

</div>

I’m getting an error when testing this on a canister deployed to the IC. Has anyone successfully gotten the http\_request method to work on the IC?

@paulyoung Using [your code](https://github.com/dfinity/agent-rs/pull/195#issue-655667255) as a reference, I was also able to serve my RSS feeds when running dfx locally. But when I deploy to a canister on the IC and load the .ic0.app page, even with a simple body like yours, I get back:

`Body does not pass verification`

The error is coming from [line 315 in validation.ts](https://github.com/dfinity/agent-js/blob/90b073dc735bfae9f3b1c7fc537bd97347c5cc68/apps/sw-cert/src/sw/http_request.ts#L315), but there are a lot of things that could cause [validateBody](https://github.com/dfinity/agent-js/blob/90b073dc735bfae9f3b1c7fc537bd97347c5cc68/apps/sw-cert/src/sw/validation.ts#L23) to return false and the error gives no hint.

Has anyone gotten a deployed app to return a successful response from the http\_request method?

---

<div class="post-metadata">

**Author:** ![mymikemiller](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/mymikemiller/32/694_2.png) [@mymikemiller](https://forum.dfinity.org/u/mymikemiller)\
**Post date:** [July 6, 2021, 11:53pm UTC](https://forum.dfinity.org/t/can-a-canister-return-generated-xml/1636/19 "2021-07-06T23:53:11Z")

</div>

@anon74414410 I’m deploying it as a motoko canister with an http\_request method that returns my generated xml, not as an asset canister. AFAIK I can’t return generated xml from an asset canister.

---

<div class="post-metadata">

**Author:** ![anon74414410](https://avatars.discourse-cdn.com/v4/letter/a/85f322/32.png) [@anon74414410](https://forum.dfinity.org/u/anon74414410)\
**Post date:** [July 6, 2021, 11:57pm UTC](https://forum.dfinity.org/t/can-a-canister-return-generated-xml/1636/20 "2021-07-06T23:57:43Z")

</div>

You’ll need to use the raw.ic0.app address. The ic0.app address expects you to to be serving certified assets, which go through a custom service worker

---

<div class="post-metadata">

**Author:** ![mymikemiller](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/mymikemiller/32/694_2.png) [@mymikemiller](https://forum.dfinity.org/u/mymikemiller)\
**Post date:** [July 7, 2021, 9:16am UTC](https://forum.dfinity.org/t/can-a-canister-return-generated-xml/1636/21 "2021-07-07T09:16:56Z")

</div>

Phenomenal, that works great!

Could you clarify on what the difference here is? .ic0.app expects to point to an asset canister serving static assets and .raw.ic0.app expects to point to a canister with an http\_request function?

[Next page](https://forum.dfinity.org/t/can-a-canister-return-generated-xml/1636.md?page=2)
