#1 2015-04-17 06:52:44

remko
Member
Registered: 2015-04-17
Posts: 4

DocVariantType.Data(1<>TDocVariant) Exception

I'm calling a REST service with mORMot like this:

function TForm9.Logon(const IPAddress: String; const Username, Password: String): Boolean;
var
  t: variant;
  json: RawUTF8;
  url: String;
  Values: TPUtf8CharDynArray;
  jsondata: TDocVariantData;
begin
  Result := False;

  url := Format('https://%s/user/login', [IPAddress]);

  TDocVariant.New(t);
  t.username := Username;
  t.password := Password;
  json := TWinHTTP.Put(url, t, 'Content-Type: application/json', True);
  jsondata := DocVariantData(_json(json).jsondata)^;
end;

This is the returned data:

{"serverinfo":{"authtype":"local","api_key":"b61c7ef7-2f44-41d3-bb3e-69681fe999b6","metricsip":"192.168.1.23","port":"8443","metricsport":"8081","ip":"192.168.1.23","version":"1.1.0.19"}}

DocVariantData fails with Exception class EDocVariant with message 'DocVariantType.Data(1<>TDocVariant)'.

function DocVariantData(const DocVariant: variant): PDocVariantData;
begin
  with TVarData(DocVariant) do
    if VType=DocVariantType.VarType then
      result := @DocVariant else
    if VType=varByRef or varVariant then
      result := DocVariantData(PVariant(VPointer)^) else
    raise EDocVariant.CreateUTF8('DocVariantType.Data(%<>TDocVariant)',[VType]);
end;

DocVariantData is null but why? Is there something invalid/unexpected in the returned JSON?

Offline

#2 2015-04-17 07:04:04

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,227
Website

Re: DocVariantType.Data(1<>TDocVariant) Exception

_json(json).jsondata tries to get the jsondata property of your returned json, which has a single property named serverinfo.
So it returns null.

Offline

#3 2015-04-17 08:38:34

remko
Member
Registered: 2015-04-17
Posts: 4

Re: DocVariantType.Data(1<>TDocVariant) Exception

Silly mistake on my side, just started exploring and took this from example. Thanks for the clear explanation.

Offline

Board footer

Powered by FluxBB