Return dynamic xml

Hi all

How to return a dynamic generated XML in Phoenix?


1 Like

    |> put_resp_content_type("text/plain")
    |> send_resp(200, "Hello world")

Try change content type

1 Like

As you return any other dynamically generated document-type.

Set the correct content type in the conn and then render the appropriate view/template.

For XML it is very easy to use the same mechanics used for HTML as well via EEx.

1 Like

The problem the response can be a json or xml. It depends on the user, what they are asking for.
I think I have to use send_resp.

1 Like

Well, the user has to specify somewhere if he wants to retrieve json or XML, may be a header field, some checkbox in a form or a fileextension in the request URL. You can branch on this. Depending on how exactly the request is made, you need to go to different places though… Haven’t done that much of phoenix so far, so I can’t tell you were exactly.

And depending on which path of the branch is taken, you emit one or the other. If you were deciding in the controller which reply format to use it could look like this:

case reply_format do
  :xml ->
    # set xml MIME-type
    # render and send back XML
  :json ->
    # set son MIME-type
    # render and send back JSON

I have to admit though, that I haven’t found a (de-)serializer for XML like Poison does for JSON…