I'm trying to get json response via HTTP GET method to a string but all I got is that:
I'm using a code like that:
memo1.Text:= idhttp1.Get('http://blabla.com/bla.php');
it returns json data. i need to get that json response to memo1.
How can I do that?
To return JSON from the server, you must include the JSON data in the body of the HTTP response message and provide a "Content-Type: application/json" response header. The Content-Type response header allows the client to interpret the data in the response body correctly.
JsonArray represents an immutable JSON array (an ordered sequence of zero or more values). It also provides an unmodifiable list view of the values in the array. A JsonArray object can be created by reading JSON data from an input source or it can be built from scratch using an array builder object.
JSON parsing is the process of converting a JSON object in text format to a Javascript object that can be used inside a program. In Javascript, the standard way to do this is by using the method JSON.
This means when you're sending JSON to the server or receiving JSON from the server, you should always declare the Content-Type of the header as application/json as this is the standard that the client and server understand.
I found a solution. That code works perfect.
function GetURLAsString(aURL: string): string;
var
lHTTP: TIdHTTP;
lStream: TStringStream;
begin
lHTTP := TIdHTTP.Create(nil);
lStream := TStringStream.Create(Result);
try
lHTTP.Get(aURL, lStream);
lStream.Position := 0;
Result := lStream.ReadString(lStream.Size);
finally
FreeAndNil(lHTTP);
FreeAndNil(lStream);
end;
end;
Try a newer version of Indy, Indy still supports non-Unicode Delphi.
The Indy TIdHTTP Get should automatially convert UTF-8 to Ansi (with limitations):
Internally, TIdHTTP.Get() decodes the server's UTF-8 data to Unicode, and then converts that to Ansi when returning it as an AnsiString in pre-2009 versions. That AnsiString uses the OS's default Ansi codepage, so yes, there is potential for data loss if the OS's current language does not support the Unicode characters being used by the server. http://forums2.atozed.com/viewtopic.php?f=7&t=3011
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With