Http Api - multiline strings

Hi

I have the following http method to post HL7 messages.

    [ROServiceMethod('HL7Message')]
    [RODocumentation('Adds a new HL7 message.')]
    [ROCustom(ApiRequestName, 'HL7Post')]
    [ROCustom(ApiPath, PATH + '/add')]
    [ROCustom(ApiResult, '201')]
    [ROCustom(ApiMethod, 'POST')]
    procedure HL7Message(const hl7: string);

A HL7 message looks like this

MSH|^~&|MESA_ADT|XYZ_ADMITTING|iFW|ZYX_HOSPITAL|||ADT^A04|103102|P|2.4||||||||
EVN||200007010800||||200007010800
PID|||583295^^^ADT1||DOE^JANE||19610615|M-||2106-3|123 MAIN STREET^^GREENSBORO^NC^27401-1020|GL|(919)379-1212|(919)271-3434~(919)277-3114||S||PATID12345001^2^M10|123456789|9-87654^NC
NK1|1|BATES^RONALD^L|SPO|||||20011105
PV1||E||||||5101^NELL^FREDERICK^P^^DR|||||||||||V1295^^^ADT1|||||||||||||||||||||||||200007010800||||||||
PV2|||^ABDOMINAL PAIN
OBX|1|HD|SR Instance UID||1.123456.2.2000.31.2.1||||||F||||||
AL1|1||^PENICILLIN||PRODUCES HIVES~RASH
AL1|2||^CAT DANDER
DG1|001|I9|1550|MAL NEO LIVER, PRIMARY|19880501103005|F||
PR1|2234|M11|111^CODE151|COMMON PROCEDURES|198809081123
ROL|45^RECORDER^ROLE MASTER LIST|AD|CP|KATE^SMITH^ELLEN|199505011201
GT1|1122|1519|BILL^GATES^A
IN1|001|A357|1234|BCMD|||||132987
IN2|ID1551001|SSN12345678

Only the content is important, there are a few markdown-tweaks (asterisks) which are normally not there.

I’m not able to send such a message in the body of the request. How can I achieve this?
I just want to send raw text data, no JSON or something else.

Pls advice.

Regards,
Filip

Hi,

works here:

Note: I’ve used curl so in my case, hl7 value should be json compatible.

try:

curl -v -X POST “http://localhost:8099/api/add” -H “accept: application/json” -H “content-type: application/json” -d @a.json

  • a.json
{
  "hl7": "MSH|^~\\\\&|MESA_ADT|XYZ_ADMITTING|iFW|ZYX_HOSPITAL|||ADT^A04|103102|P|2.4||||||||\r\nEVN||200007010800||||200007010800\r\nPID|||583295^^^ADT1||DOE^JANE||19610615|M-||2106-3|123 MAIN STREET^^GREENSBORO^NC^27401-1020|GL|(919)379-1212|(919)271-3434~(919)277-3114||S||PATID12345001^2^M10|123456789|9-87654^NC\r\nNK1|1|BATES^RONALD^L|SPO|||||20011105\r\nPV1||E||||||5101^NELL^FREDERICK^P^^DR|||||||||||V1295^^^ADT1|||||||||||||||||||||||||200007010800||||||||\r\nPV2|||^ABDOMINAL PAIN\r\nOBX|1|HD|SR Instance UID||1.123456.2.2000.31.2.1||||||F||||||\r\nAL1|1||^PENICILLIN||PRODUCES HIVES~RASH\r\nAL1|2||^CAT DANDER\r\nDG1|001|I9|1550|MAL NEO LIVER, PRIMARY|19880501103005|F||\r\nPR1|2234|M11|111^CODE151|COMMON PROCEDURES|198809081123\r\nROL|45^RECORDER^ROLE MASTER LIST|AD|CP|KATE^SMITH^ELLEN|199505011201\r\nGT1|1122|1519|BILL^GATES^A\r\nIN1|001|A357|1234|BCMD|||||132987\r\nIN2|ID1551001|SSN12345678"
}

I’ve received this json string with

procedure TForm12.Button1Click(Sender: TObject);
var
  x : TROJSONValue;
begin
  x := TROJSONValue.Create(jdtString);
  x.AsString := Memo1.Lines.Text;
  memo2.Text := x.AsJSON;
  x.Free;
end;

Strange thing…
I use Postman to test several situations. If I send the hl7 value as Text I don’t get the content of the string, sending as JSON has the correct content.

What I am trying to accomplish…
I really want the hl7 value as text. I did this before in the OnCustomResponseEvent like this:

procedure TdmServer.svrHTTPCustomResponseEvent(const aTransport: IROHTTPTransport;
  const aRequestStream, aResponseStream: TStream; const aResponse: IROHTTPResponse; var aHandled: Boolean);

const
  SErrorJSON = '{ "error" : {"class" : "%s", "message": "%s"} }';
  SApplicationJSON = 'application/json; charset=utf-8';
var
  aMsg: TStringStream;
  aError: UTF8String;
  Paths: TArray<string>;
begin
  Paths := aTransport.PathInfo.Split(['/'], TStringSplitOptions.ExcludeEmpty);

  aHandled := (Length(Paths) = 2) and SameText(Paths[0], 'hl7') and SameText(Paths[1], 'add');
  if not aHandled then
    Exit;

  aMsg := TStringStream.Create('', TEncoding.UTF8);
  try
    aMsg.CopyFrom(aRequestStream, 0);
    try
      dmHL7.ProcessHL7(aMsg);
      aResponse.Code := 200;
    except
      on E: Exception do
      begin
        // Send error back as JSON, UTF8 encoded
        aResponse.Code := 500;
        aResponse.ContentType := SApplicationJSON;
        aError := UTF8Encode(Format(SErrorJSON, [E.ClassName, DoEscape(E.Message)]));
        aResponseStream.WriteBuffer(aError[1], Length(aError));
      end;
    end;
  finally
    aMsg.Free;
  end;
end;

So, if users need to change their processes they won’t be so happy :wink:
Is there a way to accomplish this?

Hi,

Json string is usual string, except that some chars should be quoted:


your original multiline string contains quoted chars so replace them as

  • \\\
  • CRLF (#13#10) → \r\n