restcoldfusiongoogle-translate

Creating a REST interface in Coldfusion to use Google Translate API response is failing


The following block of code is in a CFC using cfscript.

        httpService = new http();
        httpService.setMethod(action);
        httpService.setCharset("utf-8");
        httpService.setUrl("https://translation.googleapis.com/language/translate/v2");
        httpService.addParam(type = "header", name = "X-Goog-Api-Key", value = "theActualAPIKey");
        httpService.addParam(type = "header", name = "Content-Type", value = "application/json");
        httpService.addParam(type = "body", value = "#body#");

        res = httpService.send().getPrefix();

The body is a JSON object.

{
  "q": ["good-bye", "Hello", "What time is it"],
  "source": "en",
  "target": "es"
}

If I run the equivalent of this with curl or via regbin.com I get back the expected results. But when run from within a Coldfusion page I get the following error and stacktrace at the last line shown above: res = httpService.send().getPrefix();

Detail The expression has requested a variable or an intermediate expression result as a simple value. However, the result cannot be converted to a simple value. Simple values are strings, numbers, boolean values, and date/time values. Queries, arrays, and COM objects are examples of complex values.

The most likely cause of the error is that you tried to use a complex value as a simple one. For example, you tried to use a query variable in a cfif tag. ErrNumber 0 Message Complex object types cannot be converted to simple values. StackTrace coldfusion.runtime.CfJspPage$ComplexObjectException: Complex object types cannot be converted to simple values. at coldfusion.runtime.Cast._String(Cast.java:1409) at coldfusion.runtime.Cast._String(Cast.java:1347) at coldfusion.tagext.net.HttpTag.setBody(HttpTag.java:1667) at coldfusion.tagext.net.HttpParamTag.doStartTag(HttpParamTag.java:219) at coldfusion.runtime.CfJspPage._emptyTcfTag(CfJspPage.java:5274) at cfbase2ecfc970638991$funcINVOKETAG.runFunction(G:\JBoss7_3\www14dev1\deployments\cfusion.ear\cfusion.war\WEB-INF\cfusion\CustomTags\com\adobe\coldfusion\base.cfc:325) at coldfusion.runtime.UDFMethod.invoke(UDFMethod.java:623) at coldfusion.filter.SilentFilter.invoke(SilentFilter.java:47) at coldfusion.runtime.UDFMethod$ReturnTypeFilter.invoke(UDFMethod.java:553) at coldfusion.runtime.UDFMethod$ArgumentCollectionFilter.invoke(UDFMethod.java:516) at coldfusion.filter.FunctionAccessFilter.invoke(FunctionAccessFilter.java:95) at coldfusion.runtime.UDFMethod.runFilterChain(UDFMethod.java:463) at coldfusion.runtime.UDFMethod.runFilterChain(UDFMethod.java:438) at coldfusion.runtime.UDFMethod.invoke(UDFMethod.java:310) at coldfusion.runtime.CfJspPage._invokeUDF(CfJspPage.java:5084) at coldfusion.runtime.CfJspPage._invokeUDF(CfJspPage.java:5064) at coldfusion.runtime.CfJspPage._invoke(CfJspPage.java:4352) at coldfusion.runtime.CfJspPage._invoke(CfJspPage.java:4290) at cfhttp2ecfc620228233$funcSEND.runFunction(G:\JBoss7_3\www14dev1\deployments\cfusion.ear\cfusion.war\WEB-INF\cfusion\CustomTags\com\adobe\coldfusion\http.cfc:103) at coldfusion.runtime.UDFMethod.invoke(UDFMethod.java:623) at coldfusion.runtime.UDFMethod$ReturnTypeFilter.invoke(UDFMethod.java:553) at coldfusion.runtime.UDFMethod$ArgumentCollectionFilter.invoke(UDFMethod.java:516) at coldfusion.filter.FunctionAccessFilter.invoke(FunctionAccessFilter.java:95) at coldfusion.runtime.UDFMethod.runFilterChain(UDFMethod.java:463) at coldfusion.runtime.UDFMethod.runFilterChain(UDFMethod.java:438) at coldfusion.runtime.UDFMethod.invoke(UDFMethod.java:310) at coldfusion.runtime.TemplateProxy.invoke(TemplateProxy.java:975) at coldfusion.runtime.TemplateProxy.invoke(TemplateProxy.java:696) at coldfusion.runtime.TemplateProxy.invoke(TemplateProxy.java:503) at coldfusion.runtime.CfJspPage._invoke(CfJspPage.java:4327) at coldfusion.runtime.CfJspPage._invoke(CfJspPage.java:4290) at

This looks like it would be an error in the response, possibly HTML when it is expecting JSON and that can happen in certain error cases but typically that is a 404 error. All others seem to be JSON. And as I've said using curl or regbin.com all is good.

I'm stumped. Of course it is probably something painfully obvious.


Solution

  • The error message indicates that the http call is expecting a simple variable, such as a string or number, but a complex variable, such as a struct or array, is being passed instead.

    The body variable included seems to be a struct, rather than a string. JSON is always a string, never an object or struct. Wrapping body with serializeJSON() should convert the struct into JSON.