+ Start a Discussion
JeffreyStevensJeffreyStevens 

Passing POST parameters in Workbench

I'm using Workbench to test an Apex Rest POST API that I'm writing.  I want to pass paramters in the request object (not in the URL).  
So..., I don't understand what to put the in the request body field on the Workbench REST Explorer.

I was thinking that all I needed was...
{
"Parm1" : "Value1" , 
"Parm2" : "Value2"
}

But when I do that - I don't see those values in the body of the request, or in the params.

My apex code is:
@HttpPost
    global static string returnClientInfo() {
    	RestRequest		request 	= RestContext.request;
    	RestResponse	response	= RestContext.response;
    	
    	string	pParam1		= request.params.get(Param1');
    	string	pParam2		= request.params.get('Param2');
    	
    	response.addHeader('Content-Type','applicatin/json');
    	 	
    	return 'returnClientInfo finished.' + pParam1 + ' ' + pParam2 + ' request=' + request + ' request.params=' + request.params;
    }

I'm getting null for both params, and {} for the response.params.

Thanks
Best Answer chosen by JeffreyStevens
Amit Chaudhary 8Amit Chaudhary 8
If you want to send param in body then you can use JSON to apex for same
1) https://www.adminbooster.com/tool/json2apex
2) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_json_jsonparser.htm

Your code should be like below
@RestResource(urlMapping='/api/Acct/*')
global with sharing class MyFirstRestAPIClass1
{
    @HttpPost
    global static string returnClientInfo() 
    {
        RestRequest     request    = RestContext.request;
        RestResponse    response   = RestContext.response;    
        response.addHeader('Content-Type','applicatin/json');

        fromJSON jsonBody ;
        try
        {
            jsonBody  = (fromJSON) JSON.deserialize(request.requestBody.toString(),fromJSON.class);
        }
        Catch(Exception ee)
        {
        }

        return 'returnClientInfo finished.' + jsonBody.Parm1+ ' ' + jsonBody.Parm2+ ' request=' + request ;
    }
    
    public class fromJSON
    {
        public String Parm1;    //Value1
        public String Parm2;    //Value2
    }
}
URL:- /services/apexrest/api/Acct
Body:-
{
"Parm1" : "Value1" ,
"Parm2" : "Value2"
}

User-added image

Let me know if this will help you

 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Your Request body should be like below

Please pass URL lile below
/services/apexrest/api/Acct?Param1='Value1'&Param2='Value2''
Body
{
}

NOTE:- you are using Param tag then you need pass the value in URL

User-added image




Let us know if this will help you
 
JeffreyStevensJeffreyStevens
Yes - I know that I can pass it in the URL, but I don't want to pass them there.  I want to pass them in the request body.

 
Amit Chaudhary 8Amit Chaudhary 8
If you want to send param in body then you can use JSON to apex for same
1) https://www.adminbooster.com/tool/json2apex
2) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_json_jsonparser.htm

Your code should be like below
@RestResource(urlMapping='/api/Acct/*')
global with sharing class MyFirstRestAPIClass1
{
    @HttpPost
    global static string returnClientInfo() 
    {
        RestRequest     request    = RestContext.request;
        RestResponse    response   = RestContext.response;    
        response.addHeader('Content-Type','applicatin/json');

        fromJSON jsonBody ;
        try
        {
            jsonBody  = (fromJSON) JSON.deserialize(request.requestBody.toString(),fromJSON.class);
        }
        Catch(Exception ee)
        {
        }

        return 'returnClientInfo finished.' + jsonBody.Parm1+ ' ' + jsonBody.Parm2+ ' request=' + request ;
    }
    
    public class fromJSON
    {
        public String Parm1;    //Value1
        public String Parm2;    //Value2
    }
}
URL:- /services/apexrest/api/Acct
Body:-
{
"Parm1" : "Value1" ,
"Parm2" : "Value2"
}

User-added image

Let me know if this will help you

 
This was selected as the best answer
JeffreyStevensJeffreyStevens
Ah yes - that was it - I needed to deserialize the request body into a class.  Thank you!
Parikhit SarkarParikhit Sarkar
The following apex class example will allow you to set parameters in the query string for a post request -

    @RestResource(urlmapping = '/sendComment/*')

    global without sharing class postComment {
  
    @HttpPost
    global static void postComment(){
               
        //create parameters 

        string commentTitle = RestContext.request.params.get('commentTitle');
        string textBody = RestContext.request.params.get('textBody');       
        
        //equate the parameters with the respective fields of the new record

        Comment__c thisComment = new Comment__c(
            Title__c = commentTitle,
            TextBody__c = textBody, 
            
        );
        
        insert thisComment; 
        
        
        RestContext.response.responseBody = blob.valueOf('[{"Comment Id": 
        '+JSON.serialize(thisComment.Id)+', "Message" : "Comment submitted 
        successfully"}]');
        }
    }


The URL for the above API class will look like - 

/services/apexrest/sendComment?commentTitle=Sample title&textBody=This is a comment