John John - 7 months ago 130
reST (reStructuredText) Question

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.

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download