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
AKM123AKM123 

Rest Http Callout setbody error

Hi
I am trying to make a http call to onedrive, I am getting error stating that I have not put grant_type parameter in the setBody although I put it

This is how I written
req.setBody('{"grant_type"="'+AuthToken+'","client_id"="'+client_id+'","redirect_uri"="'+redirect_uri+'","client_secret"="'+client_secret+'","refresh_token"="'+refresh_token+'","resource"="'+resource_id+'"}');

This is the requirement
POST https://login.microsoftonline.com/common/oauth2/token
Content-Type: application/x-www-form-urlencoded

BODY:
client_id={client_id}&redirect_uri={redirect_uri}&client_secret={client_secret} &refresh_token={refresh_token}&grant_type=refresh_token&resource={resource_id}

Thanks in advance
Amit Chaudhary 8Amit Chaudhary 8
Please check below post how to genrate JSON
1) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_json_jsongenerator.htm
2) https://developer.salesforce.com/page/Getting_Started_with_Apex_JSON

Option 1:-
You can also convert the JSON to APEX.
1) https://www.adminbooster.com/tool/json2apex

Try to create the wrapper class for your JSON with above link and try below code.
String json=		'{"grant_type"="'+AuthToken+'","client_id"="'+client_id+'","redirect_uri"="'+redirect_uri+'","client_secret"="'+client_secret+'","refresh_token"="'+refresh_token+'","resource"="'+resource_id+'"}';
		
		fromJSON= System.JSON.deserialize(json, fromJSON.class);

Option 2:-
           RestRequest req = new RestRequest(); 
            req.httpMethod = 'POST';
            req.addHeader('Content-Type', 'application/json'); // Add a JSON Header as it is validated 
            req.requestURI = '/services/apexrest/api/*/URL' ;  
            req.requestBody = Blob.valueof(JSONBody); // Add JSON body        
            RestContext.request = req;
            RestContext.response = res;

Let us know if this will help you

Thanks
Amit Chaudhary