Response format in path

56 views
Skip to first unread message

chus...@gmail.com

unread,
Jul 31, 2015, 5:32:27 AM7/31/15
to phoenix-talk
Hello,

I'm currently working on a small service written in elixir that's going to replace a legacy one originally written in java. It is completely functional now but in order to keep backwards compatibility I need to provide endpoints such that their response format is included in the path. For instance, instead of /api/v1.0/resource/1 should be /api/v1.0/resource/1.json 

Is there any way to achieve that using the Phoenix Router? I have read that the trailing format param was not added for performance reasons and because HTTP headers already enable the functionality.

Thanks!

Wendy Smoak

unread,
Jul 31, 2015, 8:59:49 AM7/31/15
to phoeni...@googlegroups.com
I'm not sure if it's exactly what you're looking for, but there was a
discussion about flexible content negotiation in this issue:

https://github.com/phoenixframework/phoenix/issues/1054

By adding a route and inspecting `conn` in the controller (just
playing with the generated Phoenix app) I can see that given a simple
GET with

`curl http://localhost:4000/api/v1.0/resource/1.json`

there's a

`path_info: ["api", "v1.0", "resource", "1.json"]`

that you might be able to use. And then instead of the `:accepts`
plug, which seems to be working off the Accepts header, you might have
an `:extension` plug of your own that figures out what they're
requesting.

That's as far as I've gotten, and $dayjob awaits. :) Hopefully one of
the experts will chime in and help us out.

-Wendy

Chris McCord

unread,
Jul 31, 2015, 10:51:07 AM7/31/15
to phoeni...@googlegroups.com
What you are after is the trailing_format_plug https://github.com/mschae/trailing_format_plug


--
You received this message because you are subscribed to the Google Groups "phoenix-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to phoenix-talk...@googlegroups.com.
To post to this group, send email to phoeni...@googlegroups.com.
Visit this group at http://groups.google.com/group/phoenix-talk.
To view this discussion on the web visit https://groups.google.com/d/msgid/phoenix-talk/CAPCVKo%2BAzYCkHSu-JNSSmDStKiQu-0PPmyRjs%2BwKfsY3fd61Eg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

chus...@gmail.com

unread,
Jul 31, 2015, 11:24:26 AM7/31/15
to phoenix-talk, ch...@chrismccord.com
Exactly what I was looking for. Thanks to both of you!

Wendy Smoak

unread,
Aug 3, 2015, 7:06:18 PM8/3/15
to phoeni...@googlegroups.com
Did you get the trailing_format_plug to work? I tried it out and ran
into some issues, which I described here:

http://wsmoak.net/2015/08/03/phoenix-trailing-format.html

Chris patched it so that it will work in a pipeline (rather than
having to be in the Endpoint and applied to all requests) but I'm
still having trouble with some of the routes.

Let us know if you got any further with it!

-Wendy

Josh Adams

unread,
Aug 4, 2015, 12:09:17 PM8/4/15
to phoeni...@googlegroups.com
In general, I suggest people use the content-type to do this negotiation.  Have you considered it?

--
You received this message because you are subscribed to the Google Groups "phoenix-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to phoenix-talk...@googlegroups.com.
To post to this group, send email to phoeni...@googlegroups.com.
Visit this group at http://groups.google.com/group/phoenix-talk.



--
[mail]
3800 Colonnade Pkwy
Suite 140
Birmingham, AL 35242
Reply all
Reply to author
Forward
0 new messages