Search code examples
htmldelphipostindydelphi-xe

How to Post to HTML Form two with Delphi Indy


I want to Post with Delphi XE10 Indy to Form 2 (name="voting_maybe") in following HTML code.

<form method="post" name="voting_yes" class="dtyrd-form" action="&#x2F;vote&#x2F;yes" id="voting_yes">
<button type="submit" name="submit" class="dtyrd-button&#x20;dtyrd-button-voting&#x20;dtyrd-button-color-green" value=""><span class="image"></span>Yes</button>
</form>

<form method="post" name="voting_maybe" class="dtyrd-form" action="&#x2F;vote&#x2F;maybe" id="voting_maybe">
<input type="submit" name="submit" class="dtyrd-button&#x20;dtyrd-button-voting&#x20;dtyrd-button-color-grey" value="Maybe"> 
</form>

<form method="post" name="voting_no" class="dtyrd-form" action="&#x2F;vote&#x2F;no" id="voting_no">
<input type="submit" name="submit" class="dtyrd-button&#x20;dtyrd-button-voting&#x20;dtyrd-button-color-red" value="No">
</form>  

I want to do this like:

PostStream := TIdMultiPartFormDataStream.Create;
try
  PostStream.AddFormField('submit','submit');

  Response := HTTPClient.Post(url, PostStream);

finally
  PostStream.Free;
end;

But how can I send the Post to Form 2 (name="voting_maybe")?


Solution

  • Simply populate a TStringList with name=value strings that are defined by the <input> fields of the voting_maybe webform, and then Post() to the URL specified by the action attribute of that webform. Each <form> element is to be posted independently of any other <form> on the same page.

    Do not use TIdMultiPartFormDataStream in this situation, as your <form> elements do not have an enctype="multipart/form-data" attribute on them. Without an explicit enctype, the default is application/x-www-form-urlencoded, which is what the TStrings version of Post() uses.

    For example:

    PostData := TStringList.Create;
    try
      PostData.Add('submit=Maybe');
      Response := HTTPClient.Post(baseurl+'/vote/maybe', PostData);
    finally
      PostData.Free;
    end;