Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

My POST request on TIdHttpServer contains strange characters, json string

I have an issue with the JSON string I get when receiving a POST request. Currently this is the way I'm reading it:

procedure TForm1.IdHTTPServer1CommandGet(AContext: TIdContext;
  ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
var
  Stream : TStream;
  S : string;
begin
  If ARequestInfo.Command = 'POST' then
  begin
    Stream := ARequestInfo.PostStream;
    if assigned(Stream) then
    begin
      Stream.Position := 0;
      S := UTF8ToAnsi(ReadStringFromStream(Stream));
    end;
  end;
end;

I tried ReadStringFromStream() alone and with UTF8ToAnsi()and AnsiToUTF8(), but I keep getting a string that looks like this:

'['#$A#9'{'#$A#9#9'"test":"bb",'#$A#9#9'"test":"aa"'#$A#9'}'#$A']'

I know it has something to do with encoding, but I don't know how to fix it.

like image 887
John Avatar asked Mar 07 '23 07:03

John


1 Answers

You do know that the hash (#) sign denotes a character value and that the dollar ($) sign denotes hexadecimal values, do you. Thus #$A means character decimal 10, which happens to mean NewLine and #9 means character 9 which is the TAB character. There is nothing unexpected in the return string. If you feed it into something that understands a NewLine without a preceding CarriageReturn it will probably look as you expected.

The debugger for exmple, uses the #-syntax for characters that cant be otherwise visually represented.

like image 102
Tom Brunberg Avatar answered Mar 10 '23 13:03

Tom Brunberg