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

How to display a html content in the HTTPResponse object in a VF Page?



I have two questions, i am trying to figure it out for a long time, please help me.


Below is my apex code,


public class Dev_Login{
    public String userName {get; set;}
    public String password {get; set;}
    public String outputText {get; set;}
    public Dev_Login() {
    public void loginFn() {

        Http http = new Http();
        HttpRequest req = new HttpRequest();
        HttpResponse res;
        try {        
            req.setHeader('Content-Type', 'application/x-www-form-urlencoded');      
            String theMessage = 'userId='+userName+'&password='+password;            
            res = http.send(req);
            outputText = res.getBody();
        } catch (Exception e) {



the response (res.getBody()) will contain a HTML content, i want to display it in the VF Page that contains the login button. How should i do it? i had the below in my vf page


<apex:outputpanel >
        <apex:outputtext rendered="true" value="{!outputText}"/>


But this just displayed the html content as a string, didnt render it as html.. Please tell me how should i do it?




My second question,


say if i get an XML in the response in this page (outputText)

i want to redirect to a new VF page where i will use that xml to display something... how should i pass this xml to that new page. that newpage will use a different controller..  so i want to pass this xml to the second page's controller and second page should be displayed... how should i do it.


Please help me with these two questions..





1) When using apex:outputText, use escape="false" in order to tell Salesforce that you really want to render html as html, and not as escaped html. Careful when you use this, because that can introduce XSS (Cross-Site Scripting) attack vectors.


2) If the parameters are few, just use parameters. For example:



public ApexPages.PageReference nextPage() {
  ApexPages nextPage = Page.nextPage;
  return nextPage;
Note that older browsers only support up to 2k characters, so if your XML is even remotely large, you'll have to choose another path.

If possible, you should use the same controller for both pages. This automatically grants a view state transfer between the pages. You'll have to include common functions to both pages, and there are some limitations on this "freebie" state transfer, but it's far better than trying to hack your own way through.

Alternatively, combine both pages into one page. Using the "rendered" attribute makes doing this relatively painless, and you could also use compositions or templates to break the page into sections.

Finally, if nothing else is viable, use a custom object with a Long Text Area. You can use a DML to delete the record when you no longer need it.



Thankyou very much sfdfox.


I am trying each one of your answers.


1) in the outputText, i put escape="false", but now i am not getting anything there, its just empty space. if i remove the escape="false" the html text is being displayed. Why the html is not getting rendered when i put escape="False" ??


Thanks and Regards,



I did a test with Google (just to prove it works). Here's my code:



<!-- displayGoogleHome -->
<apex:page controller="RemoteGoogleController" action="{!loadGoogleHome}">
    <apex:outputText escape="false" value="{!googleHome}"/>


public class RemoteGoogleController {
    public string googleHome { get; set; }
    public void loadGoogleHome() {
        Http endpoint = new Http();
        HttpRequest req = new HttpRequest();
        HttpResponse res = endpoint.send(req);
        googleHome = res.getBody();

This arbitrarily simple concoction allowed me to render the entire page. Of course, I could have used just a fragment from a AHAH-style response, or anything else, but the technique definitely works. The entire contents of the page were rendered inside the content section of the page, as expected, including the top navigation bar, the search box, and other links. Of course, without patching URLs, the search feature won't actually work, nor any of the other URLs, but I hope that this demonstrates that this feature works as advertised.



I am also working on same issue .


the html responce come and place on preious ulr position.


if you already resolve that please help me



// vf content



<apex:page Controller="paymentGateWay">

<apex:form >
<apex:pageBlock >
<apex:pageBlockSection >

<apex:inputText label="LoanApplication Number" value="{!LAN}"/>
<apex:inputText label="Amount" value="{!Amount}" />
<apex:commandButton value="Pay Now" immediate="true" action="{!goForPay}" />
<apex:outputpanel >
        <apex:outputtext rendered="true" value="{!outputflow2}"/>




// apex content





public class paymentGateWay {

    public String outputflow2{ get; set; }

 public string LAN{get;set;}
 public string Amount{get;set;}
public outputflow2 {get;set;}
  public string  goForPay() {
 string sessionid = UserInfo.getSessionId();
   string body='?txtCustomerId='+LAN+'&txtTxnAmount='+Amount;
   string body2= EncodingUtil.urlEncode(body, 'UTF-8');  
    Http h = new Http();

  // creating https request
       HttpRequest req = new HttpRequest();
    HttpResponse res = h.send(req);
    string links = res.getBody();
    //blob link= res.getBodyAsBlob();
    //string body3= EncodingUtil.urlDecode(links , 'UTF-8');
    outputflow2 =res.getBody();
    return outputflow2;


Add escape="false" to your outputText to render the HTML correctly.