REST call with String parameter with control characters

I am trying to design a RESTful service that takes a string (with control characters). Does something with that in the logic on the server, then returns a string, which may also have control characters.

 

Basically the string is a pharmacy claim in a delimited format that uses control characters for those delimiters. The logic on the server will pull the entire claim apart and process it.

 

I was thinking that a GET method could be uses but I'm used to sending content in JSON format and wondered what other normal RESTful ways there were to do this.

  • 0
  • 165
  • 2
  • 2

Answers

A common and simple approach is to Base64-encode binary data. See $System.Encryption.Base64Encode()/$System.Encryption.Base64Decode(), plus the atob and btoa functions in JavaScript (maybe not relevant in your particular case, depending on what the client is).

So if I am calling this service from an Ensemble operation that looks something like this:

Method getPatient(pRequest As RESTDemo.REST.ClaimRequest, Output pResponse As EnsLib.REST.GenericMessage) As %Status
{
  try {
    // Prepare and log the call
    // Append the claim to the URL
    Set tURL=..Adapter.URL_"/claims/"_pRequest.claimBlob    
       
    // Execute the call
    set tSC=..Adapter.GetURL(tURL,.tHttpResponse)
       
    #; Suppress HTTP status errors and just pass back the status, headers and body
    If $$$ISERR(tSC)&&'$$$StatusEquals(tSC,$$$EnsErrHTTPStatus) { Quit }
       
    // Return the response      
    Set tSC=$$$OK
    If $IsObject(tHttpResponse.Data) {
      Set tStream=tHttpResponse.Data
    } Else {
      Set tStream=##class(%GlobalBinaryStream).%New()
      Set tSC=tStream.Write(tHttpResponse.Data)  Quit:$$$ISERR(tSC)
    }
    Set pResponse=##class(EnsLib.REST.GenericMessage).%New(tStream,,tHttpResponse)
    Do pResponse.HTTPHeaders.SetAt(tHttpResponse.StatusLine,"StatusLine")
  } catch {
    Set tSC=$$$SystemError
  }
  Quit tSC
}

Would I just call $system.Encryption.Base64Encode() on the claimBlob property?

URL and URI Conversions

Try:
Set tURL=..Adapter.URL_"/claims/"_##class(%CSP.Page).EscapeURL(pRequest.claimBlob)
or
Set tURL=..Adapter.URL_"/claims/"_$zcvt($zcvt(pRequest.claimBlob,"O","UTF8"),"O","URL")

USER>w $zcvt($zcvt($c(0,1,2,1025),"O","UTF8"),"O","URL")
%00%01%02%D0%81
USER>##class(%CSP.Page).EscapeURL($c(0,1,2,1025))
%00%01%02%D0%81

See $zcvt for JSON, e.g.:

USER>w $zcvt($c(0,1,2),"O","JSON")
\u0000\u0001\u0002
USER>zw $zcvt("\u0000\u0001\u0002","I","JSON")
$c(0,1,2)
USER>{"binary":($c(0,1,2))}.%ToJSON()
{"binary":"\u0000\u0001\u0002"}