Proper use of GetAsX() method?

28 views
Skip to first unread message

Jordan Michaels

unread,
Aug 15, 2016, 5:12:13 PM8/15/16
to taffy...@googlegroups.com
Hi Folks,

While working on Taffy we have a specific method that we need to return as plain text, so we implemented a new method in the Serializers.cfc method, like so:

--------------------------
<cfcomponent extends="taffy.core.baseSerializer">

<cffunction name="getAsJson" output="false" taffy:mime="application/json;text/json" taffy:default="true" hint="serializes data as JSON">
<cfreturn serializeJSON(variables.data) />
</cffunction>

<cffunction name="getAsText" output="false" taffy:mime="application/text;text/plain" hint="serializes data as text">
<cfreturn toString(variables.data) />
</cffunction>

</cfcomponent>
--------------------------

This works great in the Taffy interface, and when we select "text" from the "Accept" drop down we get our response exactly how we want it.

Now, we're trying to use the getAsX() method so that this particular endpoint returns plaintext to all requests, but nothing we try appears to be working. Here's an example:

<cfreturn representationOf(LOCAL.response).getAsX(text/plain).withStatus(200) />
<cfreturn representationOf(LOCAL.response).getAsX("text/plain").withStatus(200) />
<cfreturn representationOf(LOCAL.response).getAsX(application/text).withStatus(200) />
<cfreturn representationOf(LOCAL.response).getAsX("application/text").withStatus(200) />
<cfreturn representationOf(LOCAL.response).getAsX(application/text;text/plain).withStatus(200) />
<cfreturn representationOf(LOCAL.response).getAsX("application/text;text/plain").withStatus(200) />

I'm don't think I'm using it right. Can anyone provide a working example of a successful implementation of the getAsX() method?

Thank you in advance for your help.

--
Kind regards,
Jordan Michaels
Vivio Technologies

Adam Tuttle

unread,
Aug 15, 2016, 7:45:24 PM8/15/16
to Taffy Users

You never call the getAsX method, you let Taffy do that for you.

The X in getAsX corresponds to what request "file extension" will imply the text response format, as an alternative to sending the Accept header (if you did send it, you should send, "application/text" or "text/plain")

Taffy interprets the request to figure out which of the supported formats (getters you have defined) is appropriate to respond with and calls the appropriate one to do the serialization.

If you want all requests to return plain text, get rid of the json getter and set Taffy:default="true" on your text getter.

You may or may not want to append .text to your request Uri to ensure the correct format is implied in the request. E.g. /foo/bar.text

If you would rather .txt then rename your getter getAsTxt... /foo/bar.txt

Hope that helps.
Adam


--
You received this message because you are subscribed to the Google Groups "Taffy Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to taffy-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jordan Michaels

unread,
Aug 15, 2016, 9:05:00 PM8/15/16
to taffy...@googlegroups.com
Hey Adam,

Thanks so much for your help with this.

We use JSON for 99% of what we're doing. Returning text is the requirement of a 3rd party API, and we have zero control of it, so we're trying to force Taffy to just return text for this one endpoint.

The endpoint we're attempting to create is a GET, and to complicate things more, we're using the Apache rewrite method, so the endpoint looks something like the following to Taffy:

/index.cfm?endpoint=/my/endpoint/location/{param1}/{param2}/{param3}/

So, I'm not sure where adding a .text extension would work there, but I'm open to exploring that more. Is that the only way to have Taffy respond in plaintext just for this single endpoint?

Your time and help is appreciated!

--
Kind regards,
Jordan Michaels
Vivio Technologies

> email to taffy-users...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

--
You received this message because you are subscribed to the Google Groups "Taffy Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to taffy-users...@googlegroups.com.

Adam Tuttle

unread,
Aug 16, 2016, 7:22:18 AM8/16/16
to Taffy Users

You could append ".text" in the rewritten URL. You may also need to leave off the trailing slashes (for this and other resources)



> For more options, visit https://groups.google.com/d/optout.
>

--
You received this message because you are subscribed to the Google Groups "Taffy Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to taffy-users+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Taffy Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to taffy-users+unsubscribe@googlegroups.com.

Jordan Michaels

unread,
Aug 16, 2016, 3:19:15 PM8/16/16
to taffy...@googlegroups.com
Nice. That works, but then I get a text file download rather than plaintext being displayed in the browser window. Makes sense, just not what we need.

We found a work-around last night that was able to do what we wanted. We created a "wrapper" outside of Taffy that used CFHTTP to call the Taffy URL (so we could use Taffy's logic) then just removed the JSON quotes using Replace(). It's kind of hackish, but it's a temporary thing that will just be needed for a few months, so it's not a big deal and it gets the job done.

Thanks so much for your help and advice here. Hopefully the notes on adding .text to the Taffy URL's will help someone else in a similar situation.
> > />
> > <cfreturn representationOf(LOCAL.response).getAsX("application/
> text").withStatus(200)
> > />
> > <cfreturn representationOf(LOCAL.response).getAsX(application/
> > text;text/plain).withStatus(200) />
> > <cfreturn representationOf(LOCAL.response).getAsX("application/
> > text;text/plain").withStatus(200) />
> >
> > I'm don't think I'm using it right. Can anyone provide a working example
> > of a successful implementation of the getAsX() method?
> >
> > Thank you in advance for your help.
> >
> > --
> > Kind regards,
> > Jordan Michaels
> > Vivio Technologies
> >
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Taffy Users" group.
> > To unsubscribe from this group and stop receiving emails from it, send an
> > email to taffy-users...@googlegroups.com.
> > For more options, visit https://groups.google.com/d/optout.
> >
>
> --
> You received this message because you are subscribed to the Google Groups
> "Taffy Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to taffy-users...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Taffy Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to taffy-users...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

--
You received this message because you are subscribed to the Google Groups "Taffy Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to taffy-users...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages