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

 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);
| improve this answer | |
  • Passing user name and password for authentication not good approach.Using oauth is recommended .Thanks – Mohith Shrivastava 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 Shrivastava Dec 27 '12 at 13:42
//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.setHeader('Content-Type','application/json');//Set the Proper Header
    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();


        //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 .

| 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 Shrivastava Dec 24 '12 at 4:09
  • Hi @MohithShrivastava. I closely follow your answers in salesforce stackexchange. I wanted to know how do I authenticate a user of an external application on the click of a button in opportunity details page and then send encrypted opp details to external web app? – MnZ Sep 16 '14 at 20:04
  • any reference you would want me to follow?. Thanks in advance – MnZ Sep 16 '14 at 20:05
  • @MNZ .It will depend on authentication mechanism of your external server .Some Servers follow BASIC ,some NTLM.Some oauth .So find out whats the authentication mechanism you need and then we can apporach solitioning – Mohith Shrivastava Sep 17 '14 at 3:03

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.