1

I am able to make an apex callout by passing the username and password in the request body.

String endpoint='https://xyz...com/';
String userID='xyz';
String password='pass';
String jsonBody ='userID=' + userID + '&password=' + password;
HTTP auth = new HTTP();
HTTPRequest r = new HTTPRequest();
r.setClientCertificateName('certificate_Name');
r.setEndpoint(endpoint);
r.setMethod('POST');   
r.setBody(jsonBody);
r.setHeader('Content-Type','application/x-www-form-urlencoded');
HTTPResponse authresp=new HttpResponse();
authresp = auth.send(r); 
system.debug(authresp);

I have a requirement to use the above same approach of passing the credential in request body but I have to do it using Named Credentials.

I did go through the link - https://developer.salesforce.com/docs/atlas.en-us.198.0.apexcode.meta/apexcode/apex_callouts_named_credentials_custom_headers_bodies.htm to learn that -

The Salesforce admin must set up the named credential to allow Apex code to construct headers or use merge fields in HTTP headers or bodies.

I am trying some approach to include the named credentials in the above code but I am do not get a success result so far. Below is what I tried:

Named Credential:

enter image description here

Code using Named Credential:

    HTTP auth = new HTTP();
    HTTPRequest r = new HTTPRequest();
    r.setEndpoint('callout:Named_CREDENTIAL');
    String jsonBody ='userID=' + '{!$Credential.Username}' + '&password=' + '{!$Credential.Password}';
r.setBody(jsonBody);
    r.setMethod('POST');   
    r.setHeader('Content-Type','application/x-www-form-urlencoded');
    HTTPResponse authresp=new HttpResponse();
    authresp = auth.send(r); 
    system.debug(authresp);

Response I got - System.HttpResponse[Status=Forbidden, StatusCode=403]

2
  • Where are you setting the body in the callout?
    – Eric
    Commented May 31, 2017 at 22:26
  • I did not set it. I am not sure how to set it when I use Named credentials.
    – SfdcBat
    Commented May 31, 2017 at 22:27

2 Answers 2

5

When using merge fields you need to actually set the body like

string bdy = '{ "username" : "{!$Credential.Username}", "password" : "{!$Credential.Pasword}"}'

req.setBody(bdy);

The format will depend on the requirements of the external system

The Release Notes May be helpful a would the Documentation

5
  • If you see the first code that runs successfully it uses - jsonBody ='userID=' + userID + '&password=' + password; , Can I use jsonBody ='userID=' + {!$Credential.Username} + '&password=' + {!$Credential.Pasword}"}; ?
    – SfdcBat
    Commented May 31, 2017 at 22:31
  • 1
    @SfdcBat - yup. They are just merge fields that the Named Credential replaces before sending to the external system
    – Eric
    Commented May 31, 2017 at 22:31
  • Thanks. I added this line in my code which was missing a body. String jsonBody ='userID=' + '{!$Credential.Username}' + '&password=' + '{!$Credential.Password}'; r.setBody(jsonBody); . However I get System.HttpResponse[Status=Forbidden, StatusCode=403]
    – SfdcBat
    Commented May 31, 2017 at 22:45
  • @SfdcBat If you still get the error then you need to validate your credentials, endpoint, body format, etc.
    – Eric
    Commented May 31, 2017 at 22:47
  • My bad. The endpoint was incorrect.
    – SfdcBat
    Commented May 31, 2017 at 22:49
0

How to see the data going through named credentials. Because as of now if I print the string it appends as it was. But I want to see the data in Username and Password

string bdy = '{ "username" : "{!$Credential.Username}", "password" : "{!$Credential.Pasword}"}'

System.debug(bdy);

output = { "username" : "{!$Credential.Username}", "password" : "{!$Credential.Pasword}"}
1
  • That is just a string which will be printed as it is. Did you try to use system.debug and print only {!$Credential.Username} without double quotes?
    – SfdcBat
    Commented Nov 2, 2017 at 12:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .