+ Start a Discussion
hsupriyahsupriya 

How to send String Variables in the HTTP request instead of actual values?

Hello, 

 

Currently, the code for my HTTPRequest looks like this and it works perfectly!

 

String strBody ='grant_type=password
& client_id=3MVG9A2kN3Bn17htdoBkg4rhC3s7_2.RuejWu40cERuwe9wW7zVaPZ.rSmqCjv0x_hbmqHim8dIEg
&client_secret=581611111111111111
&username=username@gmail.com
&password=mypassword1234vU13GM4JKSA5tVCh8UIlrNec7';

 Http http = new Http();

 HttpRequest req = new HttpRequest();
 req.setBody(strBody);
 req.setMethod('POST');
 req.setEndpoint('https://login.database.com/services/oauth2/token');

 

But when I put my username and password in string variables, and pass those variable in the requestbody, as follows,

String uname = 'username@gmail.com';

String pword = 'mypassword1234vU13GM4JKSA5tVCh8UIlrNec7';

String strBody ='grant_type=password
& client_id=3MVG9A2kN3Bn17htdoBkg4rhC3s7_2.RuejWu40cERuwe9wW7zVaPZ.rSmqCjv0x_hbmqHim8dIEg
&client_secret=581611111111111111
&username=uname
&password=pword';,

 

throws

 

error":"unsupported_grant_type","error_description":"grant type not supported

I am looking for a way where I can dynamically send the parameters.

 

Please help.

 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
hsupriyahsupriya

It has a very simple solution.

String uname = 'username@gmail.com';

String pword = 'mypassword1234vU13GM4JKSA5tVCh8UIlrNec7';

String strBody ='grant_type=password
& client_id=3MVG9A2kN3Bn17htdoBkg4rhC3s7_2.RuejWu40cERuwe9wW7zVaPZ.rSmqCjv0x_hbmqHim8dIEg
&client_secret=581611111111111111
&username='+uname+'
&password='+pword+'';

 

If you put the variable in '+ +', it will be read as a variable.