Looks good. nfJSON includes converting unicode escape sequences, using STRCONV(), too. So, this is a good example of how using a library is having an advantage over doing things from scratch. At first glimpse you have to dig into the howto and getting started with the library, but if it's matured it has such features already built into it. You could get away with a few STREXTRACT, if there was no unicode escape sequences for letters that don't really require to escape them.
What I am missing in your example is adding the error handling about the result state.
I wonder why you do IF SUBSTR(myCEP,1,1)<>" ", and not simpler IF NOT myCEP=="" or IF NOT EMPTY(myCEP). You could also do myCEP=ALLTRIM(thisform.txtCccep.Value) to remove any leading or trailing whitepace entered by the user erroneously, but likely unintended.
With JSON there is one more simple way to interpret it, by running it as Javascript. It comes with a few caveats, though...
Code:
xmlHttp = Createobject("MSXML2.ServerXMLHTTP")
CEPparaAPI="[URL unfurl="true"]https://api.postmon.com.br/v1/cep/13178-100"[/URL]
xmlHttp.Open("GET", CEPparaAPI,.F.)
* xmlHttp.setRequestHeader("Accept", "application/json") && not necessary
xmlHttp.Send()
If xmlHttp.status = 200 && ok
Local lcJSON, loJS, loJSON
lcJSON = xmlHttp.responseText
If xmlHttp.getResponseHeader('Content-Type')=='application/json'
loJS = Createobject("MSScriptControl.ScriptControl")
loJS.Language="JavaScript"
loJSON = loJS.Eval('('+lcJSON+')')
*!* ? lcJSON
*!* ? '--------------'
? loJSON.logradouro
? loJSON.bairro
? loJSON.cidade
? loJSON.estado
Else
? 'Cannot handle (yet) content type '+xmlHttp.getResponseHeader('Content-Type')
* handling of other response content types
Endif
Else
* handling of other HTTP response states, 404 "not found", for example, or 503 "service not available" etc.
? 'Response Status:'+xmlHttp.statusText
Endif
The caveats of this code are that MSScriptControl.ScriptControl uses a very old JS engine. It's sufficient to evaluate JSON into an object and as you can see also captures the é letter correctly in doing so and converting the Javascript object to a VFP object. It has both the chance and the risk of executing further JS code embedded into the JSON. I don't think of this as a threat in the first place, because I'd trust api.postmon.com.br to only ever return JS code, if that's indeed needed or useful in the JSON evaluation. Anyway, I would recommend to stay with nfJSON, as it won't allow any malicious JS code to execute, the JS that would execute with the evaluation by the MSScriptControl.ScriptControl would instead just be extracted as a value. And if something is used in the JSON, that the old Javascript engine behind the script control object does not understand, chances are nfJSON will or you at least get an extracted value and could open a feature request in atlopes Github to add that JSON feature.
I commented the "Accept" header. In this service, s long as you don't add '?format=xml' to the URL, the response Content-Type will be application/json, no matter if you say that you will accept that content-type or not. Which means what I already said: You can't and won't enforce the result content type with an Accept header. It has no influence. Which can be seen by intentionally requesting the response for an unknown or wrong CEP: The response status then is 404 and the Content-Type sent back is HTML, a HTML page displaying the error to a user, if it would be shown in a browser. So no matter that you said you "only" accept JSON as a result content-type, the server still sends you the Mime type text/html and not application/json.
Another example you already could have realized and seen is if you set Accept to application/xml the service still sends back application/json, as it only reacts to the format parameter, if it's used in the URL of the request.
Any Accept header isn't forcing the server, it's just encouraging this response content type, but a server can even ignore it, it's not binding, it just can trigger content negotiation, but it's never forced. It won't even lead the server to not send back JSON when you say you only accept application/xml but don't send the format=xml parameter. It simply is ignored and not even in the overarching processing of HTPP requests by any web server on top of what the serverside script makes of the request, is it stopping a response that's not one of the accepted content types. So in simple terms, it's like saying you accept ketchup with your fries and still you get served fries with mayonnaise.
See
and
In this service the only determining factor is the format URL parameter and the service defaults to JSON, if not explicitly asked to provide XML. In general, thinking of any API service or any webserver, accept is only helpful, if you know the server respects it and does respond differently with different Accept headers sent to it. It's not enough that a service can (obviously in this case) return both JSON and XML that this can then also be controlled by the Accept header. In this case, they decided server-side to only make the difference of what to return by the URL parameterization. Not by Accept header.
Chriss