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.
Comments
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?
Try: <FONT COLOR="#0000ff">Set </FONT><FONT COLOR="#800000">tURL</FONT><FONT COLOR="#000000">=..</FONT><FONT COLOR="#0000ff">Adapter</FONT><FONT COLOR="#000000">.</FONT><FONT COLOR="#0000ff">URL</FONT><FONT COLOR="#000000"></FONT><FONT COLOR="#008000">"/claims/"</FONT><FONT COLOR="#000000"></FONT><FONT COLOR="#000080">##class</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#008080">%CSP.Page</FONT><FONT COLOR="#000000">).</FONT><FONT COLOR="#0000ff">EscapeURL</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#800000">pRequest</FONT><FONT COLOR="#000000">.</FONT><FONT COLOR="#0000ff">claimBlob</FONT><FONT COLOR="#000000">)</FONT> or <FONT COLOR="#0000ff">Set </FONT><FONT COLOR="#800000">tURL</FONT><FONT COLOR="#000000">=..</FONT><FONT COLOR="#0000ff">Adapter</FONT><FONT COLOR="#000000">.</FONT><FONT COLOR="#0000ff">URL</FONT><FONT COLOR="#000000"></FONT><FONT COLOR="#008000">"/claims/"</FONT><FONT COLOR="#000000"></FONT><FONT COLOR="#0000ff">$zcvt</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#0000ff">$zcvt</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#800000">pRequest</FONT><FONT COLOR="#000000">.</FONT><FONT COLOR="#0000ff">claimBlob</FONT><FONT COLOR="#000000">,</FONT><FONT COLOR="#008000">"O"</FONT><FONT COLOR="#000000">,</FONT><FONT COLOR="#008000">"UTF8"</FONT><FONT COLOR="#000000">),</FONT><FONT COLOR="#008000">"O"</FONT><FONT COLOR="#000000">,</FONT><FONT COLOR="#008000">"URL"</FONT><FONT COLOR="#000000">)</FONT>
USER>w $zcvt($zcvt($c(0,1,2,1025),"O","UTF8"),"O","URL") %00%01%02%D0%81 USER>w ##class(%CSP.Page).EscapeURL($c(0,1,2,1025)) %00%01%02%D0%81
See $zcvt for JSON, e.g.:
USER><FONT COLOR="#0000ff">w $zcvt</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#0000ff">$c</FONT><FONT COLOR="#000000">(0,1,2),</FONT><FONT COLOR="#008000">"O"</FONT><FONT COLOR="#000000">,</FONT><FONT COLOR="#008000">"JSON"</FONT><FONT COLOR="#000000">)</FONT>
\u0000\u0001\u0002
USER><FONT COLOR="#0000ff">zw $zcvt</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#008000">"\u0000\u0001\u0002"</FONT><FONT COLOR="#000000">,</FONT><FONT COLOR="#008000">"I"</FONT><FONT COLOR="#000000">,</FONT><FONT COLOR="#008000">"JSON"</FONT><FONT COLOR="#000000">)</FONT>
<FONT COLOR="#0000ff">$c</FONT><FONT COLOR="#000000">(0,1,2)</FONT>
USER><FONT COLOR="#0000ff">w </FONT><FONT COLOR="#ff00ff">{</FONT><FONT COLOR="#008000">"binary"</FONT><FONT COLOR="#808080">:</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#0000ff">$c</FONT><FONT COLOR="#000000">(0,1,2))</FONT><FONT COLOR="#ff00ff">}</FONT><FONT COLOR="#000000">.</FONT><FONT COLOR="#0000ff">%ToJSON</FONT><FONT COLOR="#000000">()</FONT>
<FONT COLOR="#ff00ff">{</FONT><FONT COLOR="#008000">"binary"</FONT><FONT COLOR="#808080">:</FONT><FONT COLOR="#008000">"\u0000\u0001\u0002"</FONT><FONT COLOR="#ff00ff">}</FONT>