Return dynamic xml

Hi all

How to return a dynamic generated XML in Phoenix?

Thanks

1 Like

https://github.com/elixir-lang/plug/blob/2df667389dbaebb0e2102343c8d7a6ed4de6e280/README.md#hello-world

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

Try change content type

2 Likes

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
end

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

2 Likes