function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
wei jwei j 

How to pass parmas to external URL by POST rather than GET in controller?

Hi All,

What I need to implement:
A VF page includes a command button "Date Entry", once users click the button, a few pramas need to be past (use POST rather than GET) to external data entry website, the page also need to be redirected to external data entry website.

I googled many related solutions, they all use GET which is appended all parameters in URL, such as:
---------------------------------------------------------------------------------
public PageReference redirect2DataEntry(){
        PageReference pageRef = new PageReference('https://www.dataentry.com/login');
        pageRef.getParameters().put('username', 'abc');
        pageRef.getParameters().put('passcode', 'xyz');
        .... ....
        return pageRef;
    }
------------------------------------------------------------------------------

My question is, can we use POST (append params in HTTP header) to pass the parameters? Some one mentioned that HttpRequest class can be used to send POST request (see below), but the problem is how can I redirect to external web page at the same time by using HttpRequest class in the controller?

-----------------------------------

String endpoint = 'http://www.example.com/service'; 
String body = 'fname=firstname&lname=lastname&age=34'; 
HttpRequest req = new HttpRequest(); 
req.setEndpoint(enpoint); 
req.setMethod('POST'); 
req.setbody(body); 
Http http = new Http(); 
HTTPResponse response = http.send(req);
---------------------------------------------------------------------------------



Thanks in advance.
SaranshSaransh
Set them in your request body. Something like this ->
string body='code='+api_code+'&client_id='+
clientid+'&client_secret='+ClientSecret+'&redirect_uri='+
baseurl+'/apex/pagename'+
'&grant_type=authorization_code';