Einzelnen Beitrag anzeigen

mikel.pahl

Registriert seit: 21. Sep 2015
Ort: Karlsruhe Neureut
5 Beiträge
 
Delphi 10.2 Tokyo Professional
 
#1

post Json mit REST

  Alt 22. Okt 2018, 17:34
Ich solle per Rest eine Json-Objekt per Post an einen Server schicken. Beispiele zum empfangen von Json-Objekten gibts viele... zum Senden nicht.

Von derem Server kommt immer zurück "invalid json primitive".

Verwundert bin ich obwohl in der Komponenten ContentType := 'application/json' ist der ContentType nach dem execute wieder "application/x-www-form-urlencoded" Das passiert auch wenn ich im Designer den Requset per Hand ausführe

Vom Support dort kommt:
Zitat:
The request body should be of content type application/json and the only body being sent should be the part between the outermost {} brackets. Example: { "ApiXRequest": ... }
Das heisst wohl, dass ich das Json-Objekt nicht als Paramter schicken darf sondern als Payload. Nur wie ??

Delphi-Quellcode:
procedure TFormRESTTest.BtnAbwertenClick(Sender: TObject);

var
  jValue, jApixResponse: TJSONValue;
  jUpdates: TJSONArray;
  hash : system.hash.THashSHA2;
  AuthToken: string;
  Stringwriter : TStringWriter;
  Writer : Tjsontextwriter;
  Builder : tjsonobjectbuilder;
  Str, Zeit,Datum, DatumZeit : string;
  Ammount : string;

begin
  memo.Clear;
  Ammount:='-'+EDBetrag.Text;
  if (AccountID<>'') and (AccountID<>'keine Karte') then
  begin
    System.SysUtils.DateTimeToString(Datum,'yyyy-mm-dd',now); // Zeit im Format yyyy-mm-ddThh-nn-ssZ
    System.SysUtils.DateTimeToString(Zeit,'hh-nn-ss',now);
    DatumZeit:=Datum+'T'+Zeit+'Z';
    hash.Reset;
    AuthToken:=hash.GetHashString(AppID+'-'+AccountInfo.AccountID+'-1-'+Ammount+'-'+DatumZeit);
    Stringwriter:=TStringWriter.Create();
    Writer:=TJsonTextWriter.Create(Stringwriter);
    Builder:=TJSONObjectBuilder.Create(Writer);
    Builder
    .BeginObject
      .BeginObject('ApiXRequest')
        .Add('Hash', AuthToken)
         .....
      .EndObject
    .EndObject;
    jValue:= TJSONObject.Create;
    jValue:= TJSONObject.ParseJSONValue(Stringwriter.ToString,true);
    Builder.Free;
    Writer.Free;
    Stringwriter.Free;
    Memo.lines.Add('-> '+jValue.ToString);
    Req_Update.Params.Clear;
    RESTWay2PAy.ContentType := 'application/json';
    Req_Update.Params.AddItem('ApiXRequest',jValue.ToString,pkGETorPOST,[poDoNotEncode]);
    Req_Update.Execute;
    if Res_Update.StatusCode = 200 then
    begin
      jValue:=Res_Update.JSONValue;
      Memo.lines.Add('<- '+ jValue.ToString);
      jApixResponse:=jValue.GetValue<TJSONObject>('ApixResponse');
      AccountInfo.AccountStatus:=jApixResponse.GetValue<Integer>('Status');
      jUpdates:=jApixResponse.GetValue<TJSONArray>('Updates');
       ......
      LBLGuthaben.Caption:=BalanceStr;
      LBLGruppe.Caption:=IntToStr(AccountInfo.DepartmentID1);
      AccountInfo.PurseBalance:=strtoint(BalanceStr);
    end
    else
    begin
      ShowMessage('Fehler :'+inttostr(Res_Update.StatusCode));
      Memo.lines.Add('<- '+ Res_Update.Content);
    end;
  end;
end;
Michael Pahl
  Mit Zitat antworten Zitat