We are experiencing an issue with our HTTP Post method. We have the need to POST some information to an external server. We have created some code in visualforce to do this for us but it seems to only be functioning when we are not authenticated. What I mean by this is that unless this visualforce code is posted on a public facing webpage - the post does not function correctly.

Here is the visualforce code that we have:

<form action="https://myservice.com/myservice"
    method="post" target="_blank">
        <input type="hidden" name="productId" value="2129422189481977244397508903997509"></input> 
        <input type="hidden" name="param1" value="1234567891"></input>      
        <input type="hidden" name="param2" value="1234567891"></input>         
        <input type="hidden" name="param3" value="120"></input> 
        <input type="hidden" name="param4" value="120"></input> 
        <input type="hidden" name="param5" value="true"></input> 
        <input type="submit" value="Submit"></input>

Again this code works from any public facing page as well as when it is hosted on an entirely separate site, other than Salesforce.

I have added the URL into my Remote Site Settings as well.

Thanks in advance for the help.

Edit 1:

I know it is not working because when it is hosted on a public salesforce page or hosted on a non salesforce server I am correctly redirected to the webpage that is returned from the post. When I am authenticated - it simply stays on the page I am at, doing nothing.

Edit 2:

Wrote a script to connect to the server using the HTTP class and am receiving a 302 Redirect error when attempting to connect to the server under an authenticated user. The exact same code works as intended when run underneath a non-authenticated user (Site Guest User).

Edit 3:

I have tried using the Location header to post against the URL that is passed back - but posting against that new URL returns the exact same status code. 302. When I run this same code under a non authenticated user (public facing web page) - I am given the 200 OK response and, if posted through a form on the visualforce page - forwards the user to the correct webpage.

 if(res.getStatusCode() >= 300 && res.getStatusCode() <= 307){

        String location = res.getHeader('Location');
        if(location != null){
            req = new HttpRequest();
            req.setHeader('Content-Type', 'text/xml');
            req.setHeader('Content-Length', String.valueOf(body.length()));
            res = http.send(req);
            system.debug('Status Code: ' + res.getStatusCode());
            system.debug('2nd location: ' + res.getHeader('Location'));
            //System.assertEquals(200, res.getStatusCode());
  • how exactly does it not work? – superfell Jun 10 '14 at 18:58
  • I updated my post to respond to your question. Thank you! – Chris Jun 10 '14 at 19:03
  • Bear in mind that a 302 is not really an error, just an instruction for the calling code to go look somewhere else (as specified in the returned location header). If you move the code to the server-side and use the HttpRequest etc classes you can handle that location header but as it looks like you want to show the response page I presume that isn't convenient. Perhaps you can raise the question with the external server vendor. – Keith C Jun 10 '14 at 19:33
  • I have also tried handling this - I updated my question to contain the section of code that is handling the redirect. The big issue is that I do not get a 302 when I am not authenticated. It appears as though Salesforce is modifying my POST in some way shape or form when running under an authenticated user – Chris Jun 10 '14 at 19:39
  • 1
    Turns out if you embed a <form> tag inside of an <apex:form> tag - it modifies the values of it (including the URL you have inside of the action attribute). This was causing the page to attempt to post against the salesforce server itself instead of my external site. – Chris Jun 10 '14 at 19:55

Turns out if you embed a form tag inside of an apex:form tag - it modifies the values of it (including the URL you have inside of the action attribute). This was causing the page to attempt to post against the salesforce server itself instead of my external site.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.