[SalesForce] Redirect following POST method

I have a scenario :
A user needs to fill a form and after submitting, it should send the data of the form using POST API to some URL –

the user will need to be redirected to this page after the params passed to this URL, to this site.

I've tried to use the HttpRequest class and pass the params inside the body – But I didn't find a way to make a follow redirection to the page.

Using PageReference – don't sure how to send it using post – If I don't get wrong here – it always sent as a GET method – or not?
When I tried to send it using PageReference – it using GET method and send the params through URL – I need them to be hidden from there.

Someone can help me with this?

The first method I tried using PageRefence :
PageReference :

public PageReference doSave() {
    PageReference pageRef = new PageReference('https://www.someSite/payment/index.php/landing');
    pageRef.getParameters().put('email','it@df.com');
    pageRef.getParameters().put('name','testName');
    pageRef.getParameters().put('token','343434');
    pageRef.getParameters().put('Id','343434');



    return pageRef;
}

The Second method I tried using HttpRequest (after making the Page Refernce in comment):
HttpRequest:

public void sendData(){
    System.debug('sendData!');

    String endpoint = 'https://www.someSite/payment/index.php/landing';
    String body = 'email=sal@salv.com&name=salv&token=a108e884&Id=23456';
    HttpRequest req = new HttpRequest();
    req.setEndpoint(endpoint);
    req.setMethod('POST');
    req.setbody(body);
    Http http = new Http();
    HTTPResponse response = http.send(req); 
}

Many thanks!

Best Answer

Using PageReference is limited for such cases - So it must be done on the client side :

I used the "normal" form HTML element to submit the form with a post method, inside apex:form element.

I used apex:form to create my form with a commandButton for manipulation the data inside the controller and an onComplete tag for calling a javascript function to submit the "normal" HTML form from above.

In that way, I could achieve the desired redirection after sending data to another server with POST method. from the client side.

Full code is below: (it may be a better way for sharing the data between the inputs (normal input and apex:input)) and I will love to hear better ways.

 <script>
          function submitForm() {
          document.querySelector("#zehut").value= document.querySelector("[id$='zehutId']").value;
          document.querySelector("#name").value= document.querySelector("[id$='nameId']").value;
          document.querySelector("#email").value= document.querySelector("[id$='emailId']").value;
          document.querySelector("#token").value= document.querySelector("[id$='tokenId']").value;
          document.querySelector("#sum").value= document.querySelector("[id$='sumId']").value;


            document.querySelector("#secondForm").submit();
          }
    </script>
    <apex:form styleClass="form-horizontal" >
      <apex:input value="{!zehut}" label="Id" id="zehutId" />
      <apex:input value="{!name}" label="Name" id="nameId" />
      <apex:input value="{!email}" label="Email"   id="emailId"/>
      <apex:input value="{!token}" label="Token"   id="tokenId"/>
      <apex:input value="{!sum}" label="Sum"   id="sumId"/>


      <apex:commandButton action="{!save}" rerender="hiddenForm" oncomplete="submitForm();" value="Submit" />
    </apex:form>
    <apex:outputText id="hiddenForm">
      <form id="secondForm" action="https://www.huika.com/payment/index.php/starting" method="post"
        target="_blank" enctype="application/x-www-form-urlencoded"  style="display:none;">
        <input id="zehut" name="zehut" value="{!zehut}"/>
        <input id="name" name="name" value="{!name}"   />
        <input id="email" name="email" value="{!email}"  />
        <input id="token" name="token" value="{!token}" />
        <input id="sum" name="sum" value="{!sum}" />


      </form>
    </apex:outputText>

This post answered by another question by sfdcfox in this link :

call apex method from Form element

Related Topic