TScHttpWebRequest. Problem with parameters

Discussion of open issues, suggestions and bugs regarding network security and data protection solution - SecureBridge
Post Reply
alexbozhko
Posts: 2
Joined: Wed 20 Jan 2016 15:43

TScHttpWebRequest. Problem with parameters

Post by alexbozhko » Fri 14 May 2021 13:08

Hi!
I have faced a problem. I can't send parameters, when I post request with TScHttpWebRequest.

I have used a next code to send request:

Code: Select all

procedure TfMain.SBClick(Sender: TObject);
var
  Response: string;
  Stream: TStream;
  ScHttpWebRequest: TScHttpWebRequest;
  httpWebResponse: TScHttpWebResponse;
  ParamsLst: TStrings;
begin
  ScHttpWebRequest := TScHttpWebRequest.Create(nil);
  try
    ScHttpWebRequest.RequestUri := eURL.Text;

    Stream := TMemoryStream.Create;
    try
      ParamsLst := TStringList.Create;
      try
        ParamsLst.Add('F1_Submit');
        ParamsLst.SaveToStream(Stream);
        Stream.Position := 0;

        ScHttpWebRequest.Method := rmPost;

        ScHttpWebRequest.ContentLength := Stream.Size;
        ScHttpWebRequest.SendChunked := True;
        ScHttpWebRequest.RequestStream := Stream;
      finally
        ParamsLst.Free;
      end;

      httpWebResponse := ScHttpWebRequest.GetResponse();
      Response := httpWebResponse.ReadAsString;
      Memo1.Lines.Add(Response);
    finally
      Stream.Free;
    end;
  finally
    ScHttpWebRequest.Free;
  end;

end;

I have made a simple demo. I have sent a post request by two ways - using TScHttpWebRequest and using Indy.

Code: Select all

procedure TfMain.IndyClick(Sender: TObject);
var
  Http: TIdHTTP;
  Response: string;
  ParamsLst: TStrings;
  
begin
  Http := TIdHTTP.Create(nil);
  try
   ParamsLst:= TStringList.Create();
   try
     ParamsLst.Add('F1_Submit');
     Response := Http.Post(eURL.Text, ParamsLst);

    try
      if Response <> '' then
      begin
        Memo1.Lines.Add(Response);
      end;
    except

    end;
   finally
     ParamsLst.Free;
   end;
  finally
    FreeAndNil(http);
  end;
end;
At the server I have next code:

Code: Select all

<?php

if (!empty($_POST))
{
    // Array of post values for each different form on your page.
    $postNameArr = array('F1_Submit', 'F2_Submit', 'F3_Submit');       

    // Find all of the post identifiers within $_POST
    $postIdentifierArr = array();
       
    foreach ($postNameArr as $postName)
    {
        if (array_key_exists($postName, $_POST))
        {
             $postIdentifierArr[] = $postName;
        }
    }

    // Only one form should be submitted at a time so we should have one
    // post identifier.  The die statements here are pretty harsh you may consider
    // a warning rather than this.
    if (count($postIdentifierArr) != 1)
    {
        count($postIdentifierArr) < 1 or
            die("\$_POST contained more than one post identifier: " .
               implode(" ", $postIdentifierArr));

        // We have not died yet so we must have less than one.
        die("\$_POST did not contain a known post identifier.");
    }
        
    switch ($postIdentifierArr[0])
    {
    case 'F1_Submit':
       echo "Perform actual code for F1_Submit.";
       break;

    case 'Modify':
       echo "Perform actual code for F2_Submit.";
       break;
          
    case 'Delete':
       echo "Perform actual code for F3_Submit.";
       break;
    }
}
else // $_POST is empty.
{
    echo "Perform code for page without POST data. ";
}
?>
When I use Indy, I get "Perform actual code for F1_Submit." response.
But I always get "Perform code for page without POST data. using TScHttpWebRequest. Would you help me resolve this problem.

Thank you

ViktorV
Devart Team
Posts: 3041
Joined: Wed 30 Jul 2014 07:16

Re: TScHttpWebRequest. Problem with parameters

Post by ViktorV » Mon 17 May 2021 14:43

Hi Alex,

In order to resolve this, please try setting SendChunked property to False and write data using WruteBuffer method.
Please find the example below:

Code: Select all

var
  Buf: TBytes;
...
  ScHttpWebRequest.SendChunked := False;

  Buf := TEncoding.UTF8.GetBytes('F1_Submit');

  ScHttpWebRequest.ContentLength := Length(Buf);
  ScHttpWebRequest.WriteBuffer(Buf);
Best regards,
Viktor

Post Reply