Einzelnen Beitrag anzeigen

ScharfeMietze

Registriert seit: 5. Mär 2014
165 Beiträge
 
Delphi 10.2 Tokyo Architect
 
#17

AW: Discord "message": "Cannot send an empty message", "code": 50006

  Alt 14. Feb 2020, 10:34
Versuch das mal so. StringList statt StringStream und ein explizites Add().
Delphi-Quellcode:
procedure TForm5.btn2Click(Sender: TObject);
var
  params: TStringList;
  LIndy: TIdHTTP;
  LResponse:string;
begin
  LIndy:= TIdHTTP.Create();
  params := TStringList.Create;
  try
    params.Add('{"content": "Test"}');
    LIndy.Request.ContentType := 'application/json';
    LIndy.Request.UserAgent := 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36';
    
    LResponse := LIndy.Post(FUrl, params);
  finally
    LIndy.Free;
    params.Free;
  end;
end;
Das war meine übliche Vorgehensweise
aber leider funzt auch das nicht...
Delphi-Quellcode:
var
  params: TStringStream;
  LIndy: TIdHTTP;
  LResponse:string;
   LsList: TStringList;
const
  LContent = '{"content": "Test"}';
begin
  LIndy:= TIdHTTP.Create();
  params := TStringStream.Create(LContent, TEncoding.UTF8);
  LsList := TStringList.Create;
  try
    params.seek(0,soFromBeginning);
    LsList.DefaultEncoding.UTF8;
    LsList.Add('{"content": "Test"}');
    LIndy.Request.ContentType := 'application/json';
    LIndy.Request.UserAgent := 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36';
    LResponse:= LIndy.Post(FUrl, params);
    mmo1.Lines.Add('3 ' + LResponse);
    try
      LResponse:= LIndy.Post(FUrl, LsList);
    except
      on E : Exception do
      begin
        mmo1.Lines.Add(E.Message);
      end;
    end;
    mmo1.Lines.Add('4 ' + LResponse);
  finally
    LIndy.Free;
    params.Free;
    LsList.Free;
  end;

end;
Ergebnis
Delphi-Quellcode:
3
HTTP/1.1 400 BAD REQUEST
4
  Mit Zitat antworten Zitat