Skip to main content
Participant
Aug 24, 2018 1:42 PM UTC
Question

getHttpRequestData().content missing part of second JSON string.

I have a script that received JSON data 99.9% of the time.

The data being sent typically contains 2 JSON log entries(rarely 1 or 3).

The script that takes the JSON post is pretty simple and just writes it to disk and then later on it gets parsed.

Receiver script:

<CFTRY>

<CFIF CGI.CONTENT_TYPE EQ "APPLICATION/JSON">

<CFSET JSON_DATA = DESERIALIZEJSON(TOSTRING(GETHTTPREQUESTDATA().CONTENT))>

<CFFILE

ACTION="WRITE"

FILE="#VARIABLES.USER_INTERACTION_OUTPUT_DIRECTORY#\#VARIABLES.USER_INTERACTION_OUTPUT_FILE#_JSON_RAW.LOG"

OUTPUT="#TOSTRING(GETHTTPREQUESTDATA().CONTENT)#">

</CFIF>

<CFCATCH>

<CFFILE

ACTION="APPEND"

FILE="#VARIABLES.USER_INTERACTION_OUTPUT_DIRECTORY#\ERROR.LOG"

OUTPUT="#VARIABLES.USER_INTERACTION_OUTPUT_FILE#_JSON_RAW.LOG: #CFCATCH.MESSAGE#">

</CFCATCH>

</CFTRY>

The result that is malformed looks like this:

{"call":{"ag...<snip>......ll_complete","version":1}

version":1}

The first long line(abbreviated) is correct JSON data but the second line ONLY contains a snippet from the end of the second packet.

I know that it is not an echo of the first line because sometimes that fragment is long enough to show values that are unique from the first line.

So, why is this happening?

Is there a better way to write the code for the receiver?

Is there a setting in JVM/CF that might make this work right?

Thanks!

    This topic is closed to new replies. Start a new post to keep the conversation going.

    3 replies

    WolfShade
    Legend
    Aug 24, 2018 2:11 PM UTC

    It could be a random quote (").  Are you URLEncoding the values before putting them into a JSON array?  If any of the values contain a quote, that could truncate the data according to JSON.

    HTH,

    ^ _ ^

    Translate
    Participant
    Aug 24, 2018 4:04 PM UTC

    I don't know yet if it will help but I've changed the code so that I only ready the structure once:

    <CFSET VARIABLES.HTTP_REQUEST_DATA_CONTENT = GETHTTPREQUESTDATA().CONTENT>

         <CFSET JSON_DATA = DESERIALIZEJSON(TOSTRING(VARIABLES.HTTP_REQUEST_DATA_CONTENT))>

         <CFFILE

              ACTION="WRITE"

              FILE="#VARIABLES.USER_INTERACTION_OUTPUT_DIRECTORY#\#VARIABLES.USER_INTERACTION_OUTPUT_FILE#_JSON_RAW.LOG"

    OUTPUT="#TOSTRING(VARIABLES.HTTP_REQUEST_DATA_CONTENT)#">

    Translate
    BKBK
    Community Expert
    Community Expert
    Aug 25, 2018 11:20 AM UTC

    If the data coming in is already JSON text, then you could just do something like

    <cfif cgi.content_type eq "application/json" or cgi.content_type eq "application/x-www-form-urlencoded" or left(lCase(cgi.content_type),5) eq "text/">

        <cfset json_data = gethttprequestdata().content>

      

        <cffile action="write" file="#variables.user_interaction_output_directory#\#variables.user_interaction_output_file#_json_raw.log" output="#json_data#">

    </cfif>

    Translate