You need to sign in to do that
Don't have an account?
JeffreyStevens
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:
I'm getting null for both params, and {} for the response.params.
Thanks
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
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 URL:- /services/apexrest/api/Acct
Body:-
{
"Parm1" : "Value1" ,
"Parm2" : "Value2"
}
Let me know if this will help you
All Answers
Please pass URL lile below Body
{
}
NOTE:- you are using Param tag then you need pass the value in URL
Let us know if this will help you
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 URL:- /services/apexrest/api/Acct
Body:-
{
"Parm1" : "Value1" ,
"Parm2" : "Value2"
}
Let me know if this will help you
@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