Play Games

Search This Blog

Friday, September 25, 2020

How to get headers information in rest resource class Salesforce - Salesforce Globe For You

 How to get headers information in rest resource class Salesforce  - Salesforce Globe For You 

Solution: There is a property called 'Headers' in RestResource class which returns the headers that are received by the request.

Example:Assume we are passing the header 'Header1' with value '123' in the request.

Apex class:

@restResource(urlMapping='/getAccountInfo/*')

global class HeaderInfoInRestResource {

    @HttpGet

    global static Account getAccount() {

        RestRequest req = RestContext.request;

        RestResponse res = RestContext.response;

        Map<String,String> mapHeader = new Map<String,String>();

        mapHeader = req.Headers;

        system.debug('mapHeader'+mapHeader);

        String header1Value ='';

        if(mapHeader.containsKey('Header1')) {

            header1Value = mapHeader.get('Header1');

            system.debug('header1Value'+header1Value);

        }

        return [Select id,name from Account limit 1];

    }

}

Open workbench https://workbench.developerforce.com/login.php and sign in with your salesforce instance credentials.

Go to Utilities tab and click on 'Rest Explorer'.

Use Get Request,add the header header1 as shown in the image and then execute.

Output:



No comments:

Post a Comment