3

I'm only familiar with REST and SOAP as API standards, and I'm a novice in general, so pardon my lack of understanding of most of these concepts.

I have a web form on a third party heroku app that I want to submit Opportunity record info to from within Salesforce (probably a button on the Opp page tied to an Apex class). The web form apparently isn't set up to receive Salesforce's RESTful callouts; instead, I was told by its creator (who is unfamiliar with Salesforce) that I could POST to the web form using cURL. I looked it up and have an unclear understanding of its role in this context, unless its just another protocol like RESTful HTTP callouts.

My question is this: can I make a cURL callout (if that's even meaningful) from Salesforce using Apex? Can I use Apex's RESTful callout capability to communicate with a cURL endpoint?

Appreciate your help, and feel free to school me on my ignorance!

7

cURL is simply a command-line utility, written in C, that runs on a variety of binary platforms (Windows, Linux, Solaris, etc). Arguably, almost anything you can do with cURL you can do with Apex Code. Heroku can't readily determine a request from cURL versus Apex Code versus a telnet connection with the appropriate code copy-pasted from Notepad.

What you need to know is the method (POST), the headers required (Content-Type, Content-Length, etc), and the expected payload (XML, JSON, URL-encoded, etc). Once you know the expected format, you can then proceed to perform your POST. The tricky part is that Apex Code is a bit more "low level" than cURL, in the sense that you, as the developer, have more input into how the message is formed.

The single exception to this rule is that Apex Code doesn't handle binary data very well. It usually has to be encoded in base64 before you send it out. This will modify the types of headers you may need to use. However, unless the third party resource only accepts binary data streams (rare), there's a very likely possibility that you can indeed call this resource using Apex Code, since Apex Code uses the same HTTP standards that everything else uses (web browsers, cURL, and so on).

You should probably ask the host if they can provide valid payloads for you to examine, so you can figure out how to replicate them in Apex Code. Assuming they have some decent documentation, you should probably be able to figure out everything you need to know using just their documentation plus the salesforce.com documentation.


Given this comment:

curl -i -X POST --data "first_name=test&last_name=testlast&email=test@test.com&phone=1234567&company=Ac‌​meInc&years_in_business=3&amount=4000&dealer_id=524b2833f7494317db000001" https://example.com/webform/start/

The code you need would be approximately:

String payload = 'first_name=test&last_name=testlast&email=test@test.com&phone=1234567&company=Ac‌​meInc&years_in_business=3&amount=4000&dealer_id=524b2833f7494317db000001';
HttpRequest req = new HttpRequest();
req.setMethod('POST');
req.setEndpoint('https://example.com/webform/start/');
req.setHeader('Content-Type','application/x-www-form-urlencoded');
req.setHeader('Content-Length',String.valueOf(payload.length()));
req.setBody(payload);
Http binding = new Http();
HttpResponse res = binding.send(req);

Most likely, the pieces you missed was the Content-Type (needs to be form encoded), and possibly the Content-Length (needs to be the total size of the body). You may need to tweak this a bit to work, but this should be close.

  • Thanks for your explanation, very helpful! So here's a cURL example they gave to me for filling out their form: curl -i -X POST --data "first_name=test&last_name=testlast&email=test@test.com&phone=1234567&company=AcmeInc&years_in_business=3&amount=4000&dealer_id=524b2833f7494317db000001" https://example.com/webform/start/ I understand how to make an HTTP request and set it as POST method using Apex. Does this cURL example tell you anything about how I need to format my callout? – smohyee Feb 28 '15 at 7:03
  • @smohyee I've added some example code based on this cURL string. – sfdcfox Feb 28 '15 at 7:14
  • Hi guys what about the CURL -u parameter, how my http Request will look? – Yucel Jan 28 '16 at 16:53

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.