Search code examples
c#.netsalesforceapexcallout

Catching/viewing the http POST from Salesforce Apex Callout


Following this post I'm trying to create an Apex Callout from Salesforce to a webservice (.Net) - or something that can receieve the POST.

Being new to this I'm uncertain of how to catch the Http POST sent from Salesforce. That is I have an Apex Class in Salesforce that gets triggered on Account insert:

public class WebServiceCallout {

    @future (callout=true)
    public static void sendNotification(String name) {

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

        req.setEndpoint('http://localhost');
        req.setMethod('POST');
        req.setBody('name='+EncodingUtil.urlEncode(name, '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());
        }

    }

}

Logically the req.setEndpoint('http://localhost'); should be replaced with my IP and an acceptable port.

If I wanted to catch this POST what project would need to be built in Visual Studio (.Net)?


Solution

  • Assuming you have a public IP address that isn't going to change and the port isn't blocked by a firewall etc...

    Add the IP address to the Remote Site Settings in Salesforce. This will allow the callout to be made you your endpoint.

    You have a very wide range of options in .NET for how to handle an inbound HTTP request. Are you going to host this in IIS, or do you just want something running at the command line with a port open?

    It could be as simple as a Web Application that examines the HTTP Request and reads the name from the body.