0

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','[email protected]');
    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 protected]&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!

2
  • 2
    Why don't you first make a request and then redirect? Commented Jan 23, 2019 at 8:31
  • Its just the two methods I tried.. There is no connection between each other and each operates separately.. I mean that I tried the first one (Page Reference) alone and than the second (HTTP Request) alone - I will fix it - it is a little confusing
    – Salvation
    Commented Jan 23, 2019 at 8:59

2 Answers 2

0

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

-1

If you are submitting form through visualforce page then during submission call webservice and then from frontend through oncomplete method navigate to next url.

1
  • I'm using site - The form is not filled in the vf page - actually, the VF page is an empty page with an action to the pageRefenrce - I'm using only the controller - Its filled in FormAssembly (a service to build custom web form and from there I redirect to my site.. - I must using it - it one if the client 's requirement ) - (Need to make some manipulation on the data and therefore I don't send it from there to the destination)
    – Salvation
    Commented Jan 23, 2019 at 9:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .