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
David Roberts 4David Roberts 4 

How can I post to a web site and launch it at the same time?

I can Http post to a website from Apex
and I can navigate to a page reference from Aura.

How can I combine the two so that I open a web site while passing (not in the url) some parameters?

Is the only way to do the two steps?

SubratSubrat (Salesforce Developers) 
Hello David ,

To combine HTTP POST with passing parameters and navigating to a website from Aura, you can use a combination of Apex and JavaScript in your Aura component's controller. Here's an example of how you can achieve this:

Create an Apex method that performs the HTTP POST request and returns the response. This can be done using the HttpRequest and HttpResponse classes in Apex. Here's an example of an Apex method that performs an HTTP POST:
public static String performHttpPost(String url, Map<String, String> parameters) {
    HttpRequest request = new HttpRequest();
    request.setEndpoint(url);
    request.setMethod('POST');
    request.setHeader('Content-Type', 'application/x-www-form-urlencoded');
    
    // Set the parameters
    String body = '';
    for (String key : parameters.keySet()) {
        if (!String.isBlank(body)) {
            body += '&';
        }
        body += EncodingUtil.urlEncode(key, 'UTF-8') + '=' + EncodingUtil.urlEncode(parameters.get(key), 'UTF-8');
    }
    request.setBody(body);
    
    // Send the request
    HttpResponse response = new Http().send(request);
    
    // Return the response body
    return response.getBody();
}
In your Aura component's JavaScript controller, call the Apex method to perform the HTTP POST request and handle the response. You can use the window.open() function to open the website in a new tab or window. Here's an example:
({
    openWebsiteWithParameters: function(component, event, helper) {
        var action = component.get('c.performHttpPost');
        action.setParams({
            url: 'https://example.com',
            parameters: {
                param1: 'value1',
                param2: 'value2'
            }
        });
        
        action.setCallback(this, function(response) {
            if (response.getState() === 'SUCCESS') {
                var responseBody = response.getReturnValue();
                
                // Handle the response if needed
                
                // Open the website with the parameters
                var url = 'https://example.com?param1=' + encodeURIComponent('value1') +
                          '&param2=' + encodeURIComponent('value2');
                window.open(url, '_blank');
            } else {
                // Handle any errors
            }
        });
        
        $A.enqueueAction(action);
    }
})

If this helps , please mark this as Best Answer.
Thank you.
David Roberts 4David Roberts 4

Hello Subrat,
Thanks for your prompt response.
I'm doing something very similar although using navigation to call the web page. I didn't want to pass the parameters in the URL so that they remain secret (not exposed in the url). I decided that the web site will have to store the sent data along with a unique identifier. Then, the identifier is added to the url and, when the page is opened, the data is retrieved.

We're just testing this now...
 

var cId = component.get('v.contactId');
var todaysDate = new Date();
var uId = cId + ':' + $A.localizationService.formatDate(todaysDate, "MMM:dd:hh:mm:ss");

...
action.setParams({
            "uId": uId,
            other parameters
});

...
in the callback success path:

var pageUrl = "https://[* the web address *]" + "/?uId="+uId;
var pageReference = {
      type: 'standard__webPage',
      attributes: {
           "url": pageUrl
       }
};//pageReference
component.find("navigationService").navigate(pageReference);

I was hoping for somthing like:

var pageReference = {
       type: 'standard__webPage',
       attributes: {
        "url": pageUrl
        }
        parameters: {
           "p1" : "value1",
           "p2" : "value2"
        }

 };//pageReference

but will have to make do with two steps.