Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

How do I send post data from a visualforce page to an external webservice (say foobar.com/someLink) with some data.

share|improve this question
add comment

2 Answers

up vote 4 down vote accepted
 public static void sendRequest(){
        HttpRequest req = new HttpRequest();
        HttpResponse res = new HttpResponse();
        Http http = new Http(); 

        req.setMethod('POST' ); // Method Type
        req.setEndpoint('foobar.com/someLink'); // Server Url
        req.setHeader('Content-Type', 'application/x-www-form-urlencoded'); // Content Type
        req.setBody('userName=' + EncodingUtil.urlEncode(userName, 'UTF-8') +   
        "&password=' + EncodingUtil.urlEncode(password, 'UTF-8') + '&cardNumber=' +  
        cardNumber); // Request Parameters
        try {
            res = http.send(req);
            if(res.getBody() != null){
                // Parse Response
            }
        } catch(Exception e) {
            System.debug('error: '+ e);
        }
    } 
share|improve this answer
    
Passing user name and password for authentication not good approach.Using oauth is recommended .Thanks –  Mohith Kumar Dec 27 '12 at 13:36
    
I just gave an example @Mohit. well thanks for suggestion –  Pramod Kumar Dec 27 '12 at 13:39
    
Welcome.This was necessary as people googling sometimes see that and follow wrong practice . –  Mohith Kumar Dec 27 '12 at 13:42
add comment
//This is a common Utility Class for POST request to the  External System

public static string initiateRequest(String Body,String requestUrl){

    Http h = new Http();
    HttpRequest req = new HttpRequest();
    req.setMethod('POST');
    req.setEndpoint(requestUrl);
    req.setHeader('Content-Type','application/json');//Set the Proper Header
    req.setHeader('Accept','application/json');
    req.setBody(Body);//JSON body as String 
    Oauth1 reuest=new Oauth1();//If oauth is required implement an Oauth Class to set the Authorisation Header
    reuest.sign(req);//Call the method to sign in as per Oauth rules
    HttpResponse res=new HttpResponse();
    try{
     res=h.send(req);

     if(res.getStatusCode()!=201){

        //log the error
     }

    }catch (Exception e){
       //log the error
    }
    System.debug('RESPONSE BODY'+ res.getBody());
    return res.getBody();//Response Bosy Generated

}

Please find the above Class that i used as Utility for POST request.

The String body you will have to construct necessary JSON

The request URL is the URL of the external service

The header Authorization parameter you may set depending on whether external system uses oauth 1.0 or oauth 2.0.

From the visualforce Page you may call this above on the action of button.

Use JSON.serialise to generate JSON data from apex Object .

share|improve this answer
2  
Why are you checking for res.getStatusCode()!=201? Shouldn't you check for a 200 response? –  Ryan Guest Dec 24 '12 at 0:08
    
@RyanGuest the server response can be 201 and 200 too.Depends on how its implemented on server.So may be i can check for OR of both to be on safe side . –  Mohith Kumar Dec 24 '12 at 4:09
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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