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.

Can anyone explain how can i make a post request to some other server from apex controller and get the response back?

For example server url is -

I have to send a post request on above URL from apex controller and need the response back.

Thanks in advance

share|improve this question
did you mean to include an example server URL? I just see a dash (-) –  Ryan Guest Dec 8 '12 at 23:34

2 Answers 2

up vote 9 down vote accepted
public class AuthCallout {

 public void basicAuthCallout(){
 HttpRequest req = new HttpRequest();

 // Specify the required user name and password to access the endpoint 

 // As well as the header and header information 

 String username = 'myname';
 String password = 'mypwd';

 Blob headerValue = Blob.valueOf(username + ':' + password);
 String authorizationHeader = 'BASIC ' +
 req.setHeader('Authorization', authorizationHeader);

 // Create a new http object to send the request object 

 // A response object is generated as a result of the request   

 Http http = new Http();
 HTTPResponse res = http.send(req);

This is the basic code.Now it depends on how you are doing Callout.If you have WSDL and converted into apex and then you may use stub classes .


Document above tells how you will parse into apex classes.


Read through the document.

Similary just use setbody method to input the body and call the request URL

public static void sendNotification(String name, String city) {

    HttpRequest req = new HttpRequest();
    HttpResponse res = new HttpResponse();
    Http http = new Http();

    req.setBody('name='+EncodingUtil.urlEncode(name, 'UTF-8')+'&city='+EncodingUtil.urlEncode(city, 'UTF-8'));
    req.setCompressed(true); // otherwise we hit a limit of 32000

    try {
        res = http.send(req);
    } catch(System.CalloutException e) {
        System.debug('Callout error: '+ e);


// run WebServiceCallout.testMe(); from Execute Anonymous to test
public static testMethod void testMe() {
    WebServiceCallout.sendNotification('My Test Customer','My City');
share|improve this answer
For starters the requestMethod should be POST rather than GET –  techtrekker Dec 7 '12 at 19:36
I didn't get how to set request parameter in Post request? Will i have to set them header? can you give a sample code which is setting request parameter in Post http request? –  Pramod Kumar Dec 8 '12 at 4:28
I updated the code and you can refer for POST request how you will set –  Mohith Shrivastava Dec 8 '12 at 13:42
Thanks a lot Mohith –  Pramod Kumar Dec 9 '12 at 3:56

Assuming you want to make a post request to the URL https://www.mysite.com/myendpoint you would do the following.

Add your domain under Remote Site Settings

To send an outbound call (POST request) from Apex in salesforce, you need to add the domain to Remote Site Settings in setup. In the example above you would add https://www.mysite.com/

Create and send the request in your controller

HttpRequest req = new HttpRequest();
HttpResponse res = new HttpResponse();
Http http = new Http();


//these parts of the POST you may want to customize
req.setHeader('Content-Type', 'application/x-www-form-urlencoded');  

try {
    res = http.send(req);
} catch(System.CalloutException e) {
    System.debug('Callout error: '+ e);

Other Notes

In my example above I am simulating a form post, which is why I'm not using compression (setCompressed is set to false) and using the form content type (application/x-www-form-urlencoded). If you're doing something else, such as working with a Rest API or a SOAP API, you'll want to customize this area of the code to fit your needs.

The above example doesn't include any code for authentication, if your request needs this you will need at add this in.

If you're calling this code from an Apex trigger, you'll want to add @future (callout=true) to the method call so it runs asynchronously.

Reference Information

This is one of areas of salesforce with the best documentation. As you start to explore outbound requests I suggest you read up on the following:

Apex Web Services and Callouts

HTTP Request Class

HTTP Response Class

share|improve this answer

Your Answer


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.