function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Gage Staruch 9Gage Staruch 9 

How can I create a post request?

Hello! I'm a newbie developer. I need to create a POST request to a specified URL that triggers from our process builder through an Apex class. Ideally, the body would be something like:
 
{
  "FName": "Jeff",
  "LName": "Test",
  "Email1": "Jeff@test.com",
  "Title": "Test",
  "Phone1": "2244363251",
  "Company": "Test",
  "Country": "US",
}
I've never done a POST request, so I am pretty lost. 

ANY help would be awesome! Even a basic template to get me started. 

Thanks all!


 
Ajay K DubediAjay K Dubedi
Hi Gage,
For sending a post request to a specified URL you can use following two methods:
1. Using workbench:
 -Follow the link: https://workbench.developerforce.com/login.php
 -Login with Api 45 and produnction.
 -From utilities select REST explorer.
 -You will get link path, header option if any and request body.
For more on this please follow: https://trailhead.salesforce.com/en/content/learn/modules/api_basics/api_basics_rest
2. Using Apex code:
 -You will have to use HTTP class and its request and response class:
 -Following is the  code for making request to http://my-end-point.com/newCustomer
 
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());
  }
 }

Above all if you are beginning with salesforce integration do believe on researching stuffs over the net, this is the key for succeeding.
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi