Apex Callout Method

    0

    0

    Smooth Criminal

    apexcallouthttp

    This code defines a callback function that takes a calloutUrl as input, a calloutMethod as input, and a map of headers to values as input. It also takes a plain text body as input.

    The code first creates an Http object and sets the calloutUrl and calloutMethod parameters. It then creates a HttpRequest object and sets the endpoint and method parameters.

    Once the request is ready, the code checks to see if there is a header map defined. If there is, the code will loop through the map, setting the header values for the request.

    If the body is not blank, the code will set the body value for the request.

    Finally, the code will send the request using the Http object. The return value of the callback function will be the response from the Http object.

    Shortcut: apex_callout

    public static HttpResponse genericCallout(String calloutUrl, String calloutMethod, Map<String, String> headersMap, String body)
      {
        Http h = new Http();
        HttpRequest req = new HttpRequest();
        req.setEndpoint(calloutUrl);
        req.setMethod(calloutMethod);
    
        if(headersMap != null)
        {
          for (String eachKey : headersMap.keySet())
          {
            req.setHeader(eachKey, headersMap.get(eachKey));
          }
        }
    
        if(String.isNotBlank(body))
        {
          req.setBody(body);
        }
    
        HttpResponse res = h.send(req);
    
        return res;
      }
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Explore
    • Cookbooks
    • Playground
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.