SALESFORCE REST APEX CONFIG

 Apex callout example:

Http http = new Http(); HttpRequest request = new HttpRequest(); request.setEndpoint('https://example.com'); request.setMethod('GET'); HttpResponse response = http.send(request); // If the request is successful, parse the JSON response. if (response.getStatusCode() == 200) { // Deserialize the JSON string into collections of primitive data types. Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody()); // Cast the values in the 'animals' key as a list List<Object> animals = (List<Object>) results.get('animals'); System.debug('Received the following animals:'); for (Object animal: animals) { System.debug(animal); } }

Passing Parameters to REST callout:

Http http = new Http(); HttpRequest request = new HttpRequest(); request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals'); request.setMethod('POST'); request.setHeader('Content-Type', 'application/json;charset=UTF-8'); // Set the body as a JSON object request.setBody('{"name":"mighty moose"}'); HttpResponse response = http.send(request); // Parse the JSON response if (response.getStatusCode() != 201) { System.debug('The status code returned was not expected: ' + response.getStatusCode() + ' ' + response.getStatus()); } else { System.debug(response.getBody()); }


For Test class:

// Create the mock response based on a static resource StaticResourceCalloutMock mock = new StaticResourceCalloutMock();

// Associate the callout with a mock response Test.setMock(HttpCalloutMock.class, mock);

A callout example from trailhead:

@isTest global class AnimalsHttpCalloutMock implements HttpCalloutMock { // Implement this interface method global HTTPResponse respond(HTTPRequest request) { // Create a fake response HttpResponse response = new HttpResponse(); response.setHeader('Content-Type', 'application/json'); response.setBody('{"animals": ["majestic badger", "fluffy bunny", "scary bear", "chicken", "mighty moose"]}'); response.setStatusCode(200); return response; } }

Basic Authentication code:

String username = 'myname';

String password = 'mypwd';  

Blob headerValue = Blob.valueOf(username + ':' + password);

String authorizationHeader = 'BASIC ' +

EncodingUtil.base64Encode(headerValue);

request.setHeader('Authorization', authorizationHeader);

Authentication using Named credentials:

request.setEndpoint('callout:<Nameed credential>/services/data/v32.0');


ref:

https://trailhead.salesforce.com/en/content/learn/modules/apex_integration_services/apex_integration_rest_callouts

1 comment:

  1. The computerized, automated machining methodology can achieve reaches, holds and different actions more efficiently than human operators and traditional machines. Another element that is more and more thought of by manufacturers is the period of the use. The reason cutting ceramics and carbide tools are thought of cost-effective are fingerprint door lock waterproof outcome of|as a outcome of} they're optimal for short-term work. High-speed steel is by far essentially the most reliable materials in CNC operations. It can resist high temperatures and works completely with extraordinarily strong supplies.

    ReplyDelete

Powered by Blogger.