Hello.
I have a REST API that returns json data, but that data contains escape characters and I can't figure out why.
Here's a sample line with data from a field named "recd_on":
"[\r\n {\r\n "recd_on": "2020-04-30T07:00:00Z",\r\n ......"
Are those escape codes being added by the serializer or by the API client or somewhere else?
Thanks! Kirk
JSON Data returned from a service will be in almost certainly in UTF-8 encoding, so you need to decode the values.
If you're using wwHttp there the lDecodeUtf8 flag can do this automatically based on the content type of the message.
+++ Rick ---
Thanks Rick.
That makes sense, but I should have been more clear. The API that is returning the escape characters with the json response is my Web Connection 6.0 REST API, so I control the API output. Can I exclude those escape characters when I'm creating my json response with the ww json serializer?
Thanks again, Kirk
Yes. On the server side using:
*** Explicitly specify UTF-8 encoding
Response.Encoding = "UTF8"
in OnProcessInit()
(or in individual methods before output is generated) will encode the entire response, regardless of how you create it.
+++ Rick ---
Thanks Rick.
My OnProcessInit() method contains:
Response.Encoding = "UTF8"
REQUEST.lUtf8Encoding = .T.
but I'm still getting the escape characters in my responses.
The response header, as shown in WebSurge (using it to test my API), looks like:
RequestId: 17_96860053 Content-Length: 4343 Cache-Control: private Content-Type: application/json;charset=utf-8 Date: Mon, 16 Aug 2021 20:59:57 GMT Server: Microsoft-IIS/8.5 X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET
Thanks for your help, Kirk
How is your JSON generated? As part of a Web Connection API REST service using wwRestProcess
? That should automatically handle encoding anything as long as your FoxPro CodePage can handle the text.
Just did a quick check with one of my application, dumping some extended characters into the data:
JSON output is properly encoded in the browser.
+++ Rick ---
Thanks Rick.
I figured out what I was doing. I noticed in the TestPage method that you don't need to serialize the response. You just return the object. I was serializing the response cursor first, and then returning that object, like this.
loReturnObj = loJSerializer.Serialize("cursor:crstheapps")
USE IN crstheapps
RETURN loReturnObj
And that was causing the escape codes. I changed it to just
RETURN "cursor.crstheapps"
and it worked as expected.
So, if I want to manually serialize a response and just return that character string, how would I tell WW not to serialize the response again?
Thanks! Kirk
Why would you want to manually return JSON?
IAC, you can return a raw
response by using the options on the JsonService
instance. Please read through the Using wwRestProcess Class topic.
+++ Rick ---
Thanks for your help Rick.
And, about the manual return, I meant if I want to create my own JSON response and then just return that value, without that return value being automatically serialized. But now I don't think that will be an issue.
Thanks again, Kirk
Ok, but I don't see a scenario where that would make sense unless you have some sort of business component that creates JSON as output (which would be kinda weird since that's a output concern).
Anyway glad it's working now...
+++ Rick ---