Copy link to clipboard
Copied
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!
Copy link to clipboard
Copied
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,
^ _ ^
Copy link to clipboard
Copied
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)#">
Copy link to clipboard
Copied
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>