Malformed JSON in response body
Hi everyone,
today I'm asking your help with an issue related to the response messages sent from my production to a client software, such as Postman or SoapUI.
Issue
In the last few days I've been unable to view the responses received from my production in Postman, even if I was able to do so earlier. I'm sending the request to a remote server reachable via a Putty tunnel.
Postman Test
I'm currently testing this simple POST message:
{
"name": "John Doe",
"email": "johndoe@example.com",
"age": 30,
"gender": "Male",
"height": 175,
"weight": 70,
"address": {
"street": "123 Main Street",
"city": "Anytown",
"state": "CA",
"postalCode": "12345"
},
"medications": ["MedicineA", "MedicineB", "MedicineC", "MedicineD"]
}that returns this error every time: "Error: Parse Error: Expected HTTP/"
.png)
From the Postman Console we can see this:
.png)
This is a bit strange because I'm expecting a response from the production which has the same message body of the request.
The Business Process that processes the message is quite simple and it possesses only the following methods:
Method OnRequest(pRequest As EnsLib.HTTP.GenericMessage, Output pResponse As EnsLib.HTTP.GenericMessage) As%Status
{
Set sc = $$$OKSet OriginalStream = pRequest.Stream
Set pResponse = ..GenerateRes(OriginalStream)
Return sc
}
ClassMethod GenerateRes(OriginalStream As%Stream.Object) As EnsLib.HTTP.GenericMessage
{
// Construct HTTP responseSet tHttpResponse = ##class(%Net.HttpResponse).%New()
Set tHttpResponse.ContentType = "application/json"// define status lineSet tHttpResponse.StatusLine = "HTTP/1.1 200 OK"// Format the newJson with the correct indentationSet jsonFormatter = ##class(%JSON.Formatter).%New()
Set sc = jsonFormatter.FormatToStream(OriginalStream, .pStream)
Set pResponse = ##class(EnsLib.HTTP.GenericMessage).%New(pStream,,tHttpResponse)
Set sc = pResponse.HTTPHeaders.SetAt(tHttpResponse.StatusLine,"STATUSLINE")
Set sc = pResponse.HTTPHeaders.SetAt(tHttpResponse.ContentType, "Content-Type")
Return pResponse
}Examining what it is shown in the Visual Trace, nothing seems to be problematic.
Here is the input message: .png)
and the following is production's output that is sent back to the client software:
.png)
SoapUI Test
I cannot explain why Postman reports that the server returned a malformed message, so I also tried using SoapUI and, in this case, the output is quite interesting:
.png)
As we can see, the Header parameter "Content-Length" is lower than before (now it's 324, earlier it was 330) and the last brace '}' is missing, resulting in a malformed JSON.
After some research, I've found that other Postman users had a similar issue with the "Content-Length" header, and someone said that he resolved it by removing this header. I've tried but I cannot figure out how to fix this issue with ObjectScript.
Can anyone explain this and provide a solution?
Thank you in advance
Comments
I know that answering a question and providing your own response may seem unusual, but I won't delete this question as it could be helpful to someone in the future.
The issue was related to the method "jsonFormatter.FormatToStream", which returned "ByRef" a pStream object that, for some reason, Postman and the other client software didn't handle well.
A solution is to remove the jsonFormatter and to pass directly the OriginalStream to the function:
Set pResponse = ##class(EnsLib.HTTP.GenericMessage).%New(OriginalStream,,tHttpResponse)If you prefer to keep the JSONFormatter, just declare the pStream as an object of %Stream.FileCharacter class before using the FormatToStream method:
// Format the newJson with the correct indentationSet jsonFormatter = ##class(%JSON.Formatter).%New()
// Declare a new stream objectSet pStream = ##class(%Stream.FileCharacter).%New()
Set sc = jsonFormatter.FormatToStream(jsonACK, .pStream)
Now everything works fine.