1

I have a IdHTTPServer in delphi indy and I want to recieve a json that is send in the body.

I have this:

procedure Treportserver.IdHTTPServer1CommandGet(AContext: TIdContext;
  ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
var
  ctxt, json,msg : string;
  inifile : TStringList;
  Stream : TStream;
  S : string;
begin
  if(ArequestInfo.Document = '/favicon.ico') then
  with AResponseInfo do
  begin
    ContentType := 'text/plain; charset=utf-8';
    ContentText := '';
    WriteHeader;
    WriteContent;
    exit;
  end;

  //LOGS('Document => '+ARequestInfo.Document);
  //ctxt := ARequestInfo.FormParams;
  If ARequestInfo.CommandType = hcPOST then
  begin
    Stream := ARequestInfo.PostStream;
    if assigned(Stream) then
    begin
      Stream.Position := 0;
      S := UTF8ToAnsi(ReadStringFromStream(Stream));
    end;
  end;
  ctxt := ARequestInfo.Params.Values['report'];
  LOGS('Petición Recibida.  Reporte => '+ctxt);
  json := GetPDF(ctxt);
  with AResponseInfo do
  begin
    ContentType := 'text/plain; charset=utf-8';
    ContentText := json;
    WriteHeader;
    WriteContent;
  end;
end;

I don't know how to get the json body send by postman in this case.

enter image description here

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 2
    The JSON should be in the `ARequestInfo.PostStream`. If it is not, then Postman is not sending what you think it is sending. What do the request headers look like? And why are you using `Utf8ToAnsi()`? `ReadStringFromStream()` returns a `string`, use it as is, there is no need to convert it to ANSI. And even if you did need a conversion, `ReadStringFromStream()` has its own parameters for that purpose. – Remy Lebeau May 27 '21 at 14:16
  • 1
    On a side note: you don't need to call `WriteHeader` and `WriteContent` manually in the code you have shown. `TIdHTTPServer` will call them automatically for you after the `OnCommandGet` event handler exits. All you have to do is populate `AResponseInfo` as needed. – Remy Lebeau May 27 '21 at 15:41
  • Thanks for the comments, `ARequestInfo.PostStream` is blank because I'm not sending formdata or parameters, I'm sending a json in the body of the post. In PHP I get this using `json_decode(file_get_contents("php://input"))` not the `$_POST` variable. – Emer Alexander Alvarado Rivera May 28 '21 at 11:58
  • anyway, I move to mORMot, for me it is easier to implement and understand than indy and it works quite well for me, both with the request params and with the json body – Emer Alexander Alvarado Rivera May 28 '21 at 12:00
  • 1
    the `PostStream` should not be blank in this situation. That is why I asked you to show the request headers. The only time the `ARequestInfo.PostStream` should be blank when a body is present is when a webform is posted, so the body gets parsed into the `ARequestInfo.Params`. But that is not the case in your example. So the body should be captured as-is into the `PostStream`. Is the `OnCreatePostStream` event being fired? And which versions of Delphi and Indy are you using to begin with? – Remy Lebeau May 28 '21 at 14:10

0 Answers0