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
raz rraz r 

how to send json data form salesforce to external system using restresourse with username and password authentication

How to send json data form salesforce to external system using restresourse with username and password authentication ..?
Ruwantha  LankathilakaRuwantha Lankathilaka
First identify the authentication mechanism on the external system. Ex SSO, oAuth etc
Then use salesforce apex/outbond message to call the external authentication

To call the external system you may need to write the web service call out. Check this article about the callouts https://developer.salesforce.com/page/Apex_Web_Services_and_Callouts

use HttpRequest class to create a request
 
HttpRequest req = new HttpRequest(); 
 
  //Set HTTPRequest Method
  req.setMethod('PUT');

  //Set HTTPRequest header properties
  req.setHeader('content-type', 'image/gif');
  req.setHeader('Content-Length','1024');
  req.setHeader('Host','s3.amazonaws.com');
  req.setHeader('Connection','keep-alive');
  req.setEndpoint( this.serviceEndPoint + this.bucket +'/' + this.key);
  req.setHeader('Date',getDateString()); 

  //Set the HTTPRequest body	
  req.setBody(body); 	

  Http http = new Http();
  
   try {
 
        //Execute web service call here		
        HTTPResponse res = http.send(req);	

        //Helpful debug messages
        System.debug(res.toString());
        System.debug('STATUS:'+res.getStatus());
        System.debug('STATUS_CODE:'+res.getStatusCode());
		
} catch(System.CalloutException e) {
	//Exception handling goes here....
}

Please make sure to mark this as the best answer if this help you, as it could help more people in the community in future.
raz rraz r
Hi 
Ruwantha Lankathilaka,

Thanks for your quick response. The above method is ok. but I would like to do using @Restresouse and @HttpPost Annotations. How can we do in this way.

Thanks in Advance!

Regards,
Riaz.
Ruwantha  LankathilakaRuwantha Lankathilaka
The @RestResource annotation is used at the class level and enables you to expose an Apex class as a REST resource. Not used for call out. Same goes to the HttpPost

By your reply I assume you want to expose your class as a web service ? if so follow the instruction given bellow

first create a connected app and enable OAuth
https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/intro_understanding_web_server_oauth_flow.htm

then create a webservice class
https://developer.salesforce.com/page/Creating_REST_APIs_using_Apex_REST

first try a simple example as bellow
 
@RestResource(urlMapping='/MyWebService/*')
global with sharing class MyTest {

    @HttpGet
    global static List<Account> getAccount(){
        return [SELECT Id,Name FROM Account LIMIT 10];
    }
}


This should be able to access via following URL

https://na8.salesforce.com/services/apexrest/MyWebService

To make this work I recomend you to try with postman 
https://chrome.google.com/webstore/detail/postman/fhbjgbiflinjbdggehcddcbncdddomop?hl=en


Please make sure to mark this as the best answer if this help you, as it could help more people in the community in future.