JSON parsing failure in ColdFusion REST API
Are you using REST API capabilities buit in into ColdFusion? It comes handy when you want to implement a fast and reliable backend for Angular or React application, it also helps a lot with cross-site integrations.
Recently we came accros the strange issue on one of the new servers. We created new REST API in ColdFusion and some of the requests were working fine. Some of them, however, were returning a strange message:
{ "Message": "coldfusion.runtime.JSONUtils$JSONParseOverflowException: JSON parsing failure: Unexpected end of JSON string" }
The error explained
It surfaced that our GET requests are working fine. The problem was with PUT and POST requests.
In PUT and POST requests, the data can be send the typical way (cURL users – as “data”) or as binary package (cURL users – data-binary). The ColdFusion engine is taking the binary data and converts JSON to the data structure “on the flight”. This is the source of the issue. The ColdFusion engine is having problems parsing JSON sent in “data-binary” part of the request.
Our solution
The interesting thing was that our REST API was working fine on one of our servers and failing on the other. It took some time to find the difference. In the Application.cfc of our problematic server, we found this:
getHttpRequestData()
In theory, it should make the request body available to ColdFusion pages. The problem is that this function has one parameter – includeBody
which is set to True by default. The documentation states that once the body is retrieved, it cannot be read again.
This was the source of the problem on our end. Once the body was retrieved, it was not available for REST API. We had to change the call to the following:
getHttpRequestData(False)
Side note
The getHttpRequestData() call was there in purpose. Request body is required for some other engines on our server. This means that the adjustment we made should be made only for particular parts of the code.
It looks that also Ben Nadel came across this issue and explained it in a more in-depth article on his blog: getHttpRequestData() May Break Your Request In ColdFusion But getHttpRequestData(False) May Not