How to make a post request to some other server from apex controller

public class AuthCallout {

 public void basicAuthCallout(){
 HttpRequest req = new HttpRequest();
 req.setEndpoint('http://www.yahoo.com');
 req.setMethod('GET');

 // 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 ' +
 EncodingUtil.base64Encode(headerValue);
 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);
 System.debug(res.getBody());
  }
 }

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 .

https://login.salesforce.com/help/doc/en/code_wsdl_to_package.htm

Document above tells how you will parse into apex classes.

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_callouts.htm

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.setEndpoint('http://my-end-point.com/newCustomer');
    req.setMethod('POST');
    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);
        System.debug(res.toString());
    }

}

// run WebServiceCallout.testMe(); from Execute Anonymous to test
public static testMethod void testMe() {
    WebServiceCallout.sendNotification('My Test Customer','My City');
}

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();

req.setEndpoint('https://www.mysite.com/myendpoint');
req.setMethod('POST');

//these parts of the POST you may want to customize
req.setCompressed(false);
req.setBody('key1=value1&key2=value2');
req.setHeader('Content-Type', 'application/x-www-form-urlencoded');  

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

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


Starting in Spring 2015, you can use Named Credentials. These support different types of authentication, including Anonymous and HTTP Basic. As a bonus, Named Credentials do not have to be configured in the Remote Site setup area.

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

req.setEndpoint('callout:My_Named_Credential/some_path');
req.setMethod('POST');
req.setHeader('Content-Type', 'application/x-www-form-urlencoded');  
req.setBody('key1=value1&key2=value2');

try {
    res = http.send(req);
    if (res.getStatusCode() == 200) {
        System.debug('Success!');
    } else {
        System.debug('HTTP error: ' + res.getStatusCode());
    }
    System.debug(res.getBody());
} catch(System.CalloutException e) {
    System.debug('Callout error: '+ e);
}

This will allow for you to configure different in different environments (Sandbox vs Production) without having to change any code or create custom settings.

Tags:

Apex