JSON check on CFC

I have a CFC (call it proxy.cfc) which I use as a proxy for the simple API that I wrote. Everything is going well, and several partners are starting to use the API effectively.

However, one site that is trying to send data does not send valid JSON, and I cannot seem to gracefully handle this error.

A valid JSON string hosted as a URL might look like this:

{"apicomponent":"proxyRemoteAdd","apimethod":"add","apiarguments":{"ph_num":1212,"rbpid":999,"ph_exch":555,"state":"HI","address_1":"123 Main Street","address_2":"","rmtid":"PON83","last_name":"Smith","test":1,"zip":999999,"first_name":"Joe","email":"test@test.com","city":"Honolulu","type":"SP","ph_area":995},"apiauthkey":"abc123"}

And it works great.

However, if this line is truncated for any reason:

{"apicomponent":"proxyRemoteAdd","apimethod":"add","apiarguments":{"ph_num":1212,"rbpid":999,"ph_exch":555,"state":"HI"

I will catch the exception as follows: Exception: JSON parsing error: Unexpected end of JSON string

This comes from my onError in Application.cfc. I added code to isolate it in Application.cfc as follows:

<cfif ARGUMENTS.EXCEPTION.MESSAGE IS "JSON parsing failure: Unexpected end of JSON string">
 <!--- do some stuff here ---> 
</cfif>

? - proxy.cfc, ? , Application.cfc proxy.cfc.

UPDATE - . , :

<cfhttp url="https://www.domain.com/api/proxy.cfc" method="post" result="httpResult" charset="UTF-8">
  <cfhttpparam type="url" name="method" value="apiauth"/>
  <cfhttpparam type="url" name="argumentCollection" value="#jsData#"/>
</cfhttp>

"apiauth" - CFC, . JSON, . ( CFC), , AP, JSON, apiarguments, , apicomponent.

CFC :

<cffunction name="apiauth" access="remote" returntype="any" output="false" returnFormat="JSON">
    <cfargument name="apicomponent" required="yes" type="string"/>
    <cfargument name="apimethod" required="yes" type="string"/>
    <cfargument name="apiauthkey" required="yes" type="string"/>
    <cfargument name="apiarguments" required="yes" type="struct"/>
    <cfset var LOCAL = {}/>

    <cfif not isDefined("ARGUMENTS.apiauthkey")>
      <cfreturn THIS.NewErrorResponse("Error 401 Malformed Request.") />
    </cfif>
    <cfif not isDefined("ARGUMENTS.apicomponent")>
      <cfreturn THIS.NewErrorResponse("Error 402 Malformed Request.") />
    </cfif>     
    <cfif not isDefined("ARGUMENTS.apimethod")>
      <cfreturn THIS.NewErrorResponse("Error 403 Malformed Request.") />
    </cfif>

        <cfset LOCAL.checkpwResult = FALSE/>
        <cfset LOCAL.apicomponent = ARGUMENTS.apicomponent />
        <cfset LOCAL.apimethod = ARGUMENTS.apimethod />

API #, , API. , /:

<cfinvoke component="#LOCAL.apicomponent#" method="#LOCAL.apimethod#" argumentcollection="#apiarguments#" returnvariable="LOCAL.Response.Data"/>

JSON , CFC Application.cfc. cfmail proxy.cfc, , .

:

coldfusion.runtime.JSONUtils $JSONParseOverflowException: JSON: JSON coldfusion.runtime.JSONUtils $ParserState.incrementOffset(JSONUtils.java:1999) coldfusion.runtime.JSONUtils $ParserState.incrementOffset(JSONUtils.java:1980) coldfusion.runtime.JSONUtils.parseString(JSONUtils.java:1385) coldfusion.runtime.JSONUtils.parseObject(JSONUtils.java:1074) coldfusion.runtime.JSONUtils.parseStruct(JSONUtils.java:1178) coldfusion.runtime.JSONUtils.parseObject(JSONUtils.java:1059) coldfusion.runtime.JSONUtils. parseStruct (JSONUtils.java:1178) coldfusion.runtime.JSONUtils.parseObject(JSONUtils.java:1059) coldfusion.runtime.JSONUtils.parseJSON(JSONUtils.java:1028) coldfusion.runtime.JSONUtils.deserializeJSON(JSONUtils.java: 168) at coldfusion.runtime.JSONUtils.deserializeJSON(JSONUtils.java:128) coldfusion.filter.FilterUtils.GetArgumentCollection(FilterUtils.java:50) coldfusion.filter.ComponentFilter.invoke(ComponentFilter.java:193) coldfusion.filter.ApplicationFilter.invoke(ApplicationFilter.java:442) coldfusion.filter.RequestMoni torFilter.invoke(RequestMonitorFilter.java:48) coldfusion.filter.MonitoringFilter.invoke(MonitoringFilter.java:40) coldfusion.filter.PathFilter.invoke(PathFilter.java:112) coldfusion.filter.ExceptionFilter.invoke(ExceptionFilter.java:94) coldfusion.filter.ClientScopePersistenceFilter.invoke(ClientScopePersistenceFilter.java:28) coldfusion.filter.BrowserFilter.invoke(BrowserFilter.java:38) coldfusion.filter.NoCacheFilter.invoke(NoCacheFilter.java:58) coldfusion.filter.GlobalsFilter.invoke(GlobalsFilter.java:38) coldfusion.filter.DatasourceFilter.invoke(DatasourceFilter. java: 22) at coldfusion.xml.rpc.CFCServlet.invoke(CFCServlet.java:155) coldfusion.xml.rpc.CFCServlet.doPost(CFCServlet.java:331) javax.servlet.http.HttpServlet.service(HttpServlet.java: 641) javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

+3
1

JSON, ( Application.cfc), apiAuth, isJSON() - JSON , isJSON(). , API , .

, , , .

, JSON . , , , ...

0

All Articles