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
IC-TannerIC-Tanner 

Saleforce sites as listener/handler for http POST?

Hi everyone,

 

Does anyone have any experience/example code with setting a sites page to function as a listener/handler for an http POST?  

 

Thanks! 

Best Answer chosen by Admin (Salesforce Developers) 
paul-lmipaul-lmi

it's fairly simple.  in the visualforce page's controller, you just end up using the

 

ApexPages.CurrentPage().getParameters().get("param") method for all the params you need.  I found that this works for both HTTP GET and POST handling in my testing.

All Answers

paul-lmipaul-lmi

it's fairly simple.  in the visualforce page's controller, you just end up using the

 

ApexPages.CurrentPage().getParameters().get("param") method for all the params you need.  I found that this works for both HTTP GET and POST handling in my testing.

This was selected as the best answer
IC-TannerIC-Tanner
Thanks Paul! Much appreciated. 
eyewelleyewell

What paul-lmi suggests will work great when you are looking for name value pairs.

But in my case, where I want to receive an XML POST via HTTPS, is there a way for me to see the whole HTTP POST message?

 

In other  programming languages, there would be a function to get the HTTPRequest content... I can't seem to find a way to get the entire message.

 

Can anyone provide me with some ideas to try?

Thanks!

Erik

paul-lmipaul-lmi
look at the apex documentation in regards to HTTP and HTTPRequest. I know there's a setBody() method, so I'm presuming there's a getBody() one too.
ErikTheTinkererErikTheTinkerer

Thanks Paul-lmi,

 

I can call the getParameters function to see this stuff:

MAP<String,String> pmtrs= ApexPages.currentPage().getParameters();

For others out there accepting an XML message via post, here's my plan

  1. Grab the parameters string using getParameters
  2. then search for the beginning of the XML:  <?xml version
  3. And work from there... 

I will keep you posted.

 

Update: So close, but this doesn't work: getParameters() appears to only be able to return a Map of name value pairs. And since an XML string is not Name Value, you just get a null string back... bummer

Here is my supporting evidence: debug logs of a CGI post vs an XML post:

 

 

CGI/Name-Value pair example:
Here is the debug log for the sendings system:
[03-12-09 13:56:42][DEBUG]: MultiServerMode is off and CategoryMode is off; running ALL scheduled engines
[03-12-09 13:56:42][INFO]: XSL file name: 1259877402.xsl
[03-12-09 13:56:42][WARN]: Required credentials not available for BASIC <any realm>@edy2007-developer-edition.na3.force.com:80
[03-12-09 13:56:42][WARN]: Preemptive authentication requested but no default credentials available

// the part below is a well formatted HTTP POST CGI message

[03-12-09 13:56:42][DEBUG]: >> "POST / HTTP/1.1[\r][\n]"
[03-12-09 13:56:42][DEBUG]: >> "User-Agent: Jakarta Commons-HttpClient/3.0.1[\r][\n]"
[03-12-09 13:56:42][DEBUG]: >> "Host: edy2007-developer-edition.na3.force.com[\r][\n]"
[03-12-09 13:56:42][DEBUG]: >> "Content-Length: 1439[\r][\n]"
[03-12-09 13:56:42][DEBUG]: >> "Content-Type: application/x-www-form-urlencoded[\r][\n]"
[03-12-09 13:56:42][DEBUG]: >> "[\r][\n]"
[03-12-09 13:56:42][DEBUG]: >> "B2_MailCity=&REAgentPhone=&FirstMortgageBalance
…..

And here is what getParameters finds:
ApexPages.currentPage()getParameters() ={AdditionalInformation=, B2_BirthDate=, B2_FirstName=, B2_GrossMonthlyIncome=, B2_LastName=, B2_MailCity=, B2_MailState=, B2_MailStreetAddress=, B2_MailZipCode=, B2_SSN=, ...}

//the values are blank, but at least it finds the data. (the data is returned in a alphabetically

//organized Map, so the A, and Bs come first... there is data in later variables...

 

XML posting example:
Here is the debug log for the sendings system:
[03-12-09 13:45:18][DEBUG]: MultiServerMode is off and CategoryMode is off; running ALL scheduled engines
[03-12-09 13:45:18][INFO]: XSL file name: 1259876718.xsl
[03-12-09 13:45:19][WARN]: Required credentials not available for BASIC <any realm>@edy2007-developer-edition.na3.force.com:80
[03-12-09 13:45:19][WARN]: Preemptive authentication requested but no default credentials available

// the part below is a well formatted HTTP POST XML message

[03-12-09 13:45:19][DEBUG]: >> "POST / HTTP/1.1[\r][\n]"
[03-12-09 13:45:19][DEBUG]: >> "Content-Type: text/xml[\r][\n]"
[03-12-09 13:45:19][DEBUG]: >> "User-Agent: Jakarta Commons-HttpClient/3.0.1[\r][\n]"
[03-12-09 13:45:19][DEBUG]: >> "Host: edy2007-developer-edition.na3.force.com[\r][\n]"
[03-12-09 13:45:19][DEBUG]: >> "Content-Length: 2304[\r][\n]"
[03-12-09 13:45:19][DEBUG]: >> "[\r][\n]"
[03-12-09 13:45:19][DEBUG]: >> "<?xml version="1.0" encoding="UTF-8"?>[\n]"
[03-12-09 13:45:19][DEBUG]: >> "<Leads xmlns="http://www.<removedtoprotecttheinnocent>.com/hla/2006">[\n]"
[03-12-09 13:45:19][DEBUG]: >> "<Lead>[\n]"
[03-12-09 13:45:19][DEBUG]: >> "<LeadType>1</LeadType>[\n]"
[03-12-09 13:45:19][DEBUG]: >> "<LeadID>222838775</LeadID>[\n]"
...

And here is what getParameters finds:
ApexPages.currentPage()getParameters() ={} <--- LOOK HERE ***** The braces {} are empty!

 

And I am unable to find the getBody() function apparently we all desire: there is an ideaexchange idea to allow access to Raw XML post data: 

http://ideas.salesforce.com/article/show/10093914/Enable_Access_to_Raw_Post_Data_in_Visualforce_Pages

 

Rock the vote!

 

Erik

Message Edited by ErikTheTinkerer on 12-03-2009 08:32 PM
ArunrajArunraj

Hello,

 

We are developing an XML listener (service) as a Sites page.

We are using getContent() to obtain the XML content posted (via HTTP POST) without parameters. To be more specific, this is not through a form POST.

 

When I call xmlBody.toString() based on the code below, it returns nothing.

 

VF page Coding

 

<apex:page controller="InboundProcessor" action="{!init}" showHeader="false" sidebar="false"></apex:page>

 

ApexClass Coding

 

public PageReference init() {  
   PageReference xml = new PageReference(ApexPages.currentPage().GetURL());
   Blob xmlBody = xml.getContent();

   System.debug('blobcontent::'+xmlBody);
   System.debug('xmlStringcontent::'+xmlBody.toString());

}

 

Is there any issue with getContent()?

 



Arunraj

carlvcarlv

How'd you guys go getting this working? That is, a sites page able to receive an xml file and parse it?

inbaljS2inbaljS2

Did  any one figure this out?

I also  need to create a listner to an HTTPPost response.

 

DLJDLJ

I used content type 'application/x-www-form-urlencoded' in my HTTP POST (sending from Excel VBA test code) and then did the following to get the body in Salesforce:

 

string body = '';

MAP<string, string> params = ApexPages.currentPage().getParameters();
for (string paramname : params.keySet() )
 body += paramname + '=' + params.get(paramname) + '\n';
if (body.endsWith('=\n')) {
 body = body.substring(0,body.length()-2);
}

 

 I do not get any parameters when I set the content-type of the HTTP POST to 'text/xml'. Does anybody know how to get it working with 'text/xml'?

CloudConversionCloudConversion

We are trying to do something similar, but need to look at the Parameter and then respond back with a specific XML response based on the type of request.  Is this possible?

 

Here is an example of our VisualForce page:

 

 

<apex:page showHeader="false" contentType="text/xml" controller="HttpController" action="{!processRequest}"> 

<apex:outputPanel rendered="{!action='getmodule'}">
	<Response moduleVersion="3.0.1" schemaVersion="1.0.0" >
		<Module>
			<Platform>Force.com</Platform>
			<Capabilities>
			    <DownloadStrategy>ByModifiedTime</DownloadStrategy>
			</Capabilities>
		</Module>
	</Response>
</apex:outputPanel>
<apex:outputPanel rendered="{!action='getcount'}">
	<Response moduleVersion="3.0.1" schemaVersion="1.0.0" >
		<count>1</count>
	</Response>
</apex:outputPanel>
</apex:page>

 

Or, maybe we can set the HTTP response in the controller?

 

 

Thanks,
Jon