We use the RestResponce class to pass data from an apex RESTful web service method to an HTTP response.
Below are the method available with RestResponse class.
1) responseBody
Returns or sets the body of the response.
The return type is of Type: Blob.
2) headers
Returns the headers to be sent to the response.
The return type is of Type: Map<String, String>.
3) statusCode
Returns or sets the response status code.
The return type is of Type: Integer.
Note: If we set the RestResponse.statusCode property to a value that's not listed in below table then an HTTP status of 500 is returned with the error message “Invalid status code for HTTP response: nnn” where nnn is the invalid status code value.
Status Code Description
200
OK
201
CREATED
202
ACCEPTED
204
NO_CONTENT
206
PARTIAL_CONTENT
300
MULTIPLE_CHOICES
301
MOVED_PERMANENTLY
302
FOUND
304
NOT_MODIFIED
400
BAD_REQUEST
401
UNAUTHORIZED
403
FORBIDDEN
404
NOT_FOUND
405
METHOD_NOT_ALLOWED
406
NOT_ACCEPTABLE
409
CONFLICT
410
GONE
412
PRECONDITION_FAILED
413
REQUEST_ENTITY_TOO_LARGE
414
REQUEST_URI_TOO_LARGE
415
UNSUPPORTED_MEDIA_TYPE
417
EXPECTATION_FAILED
500
INTERNAL_SERVER_ERROR
503 SERVER_UNAVAILABLE
Now, let us try to understand with an example.
Let us try to call the below webservice from workbench.
Below is an example of webservice which need id or any unique identifier as a parameter in request and it will return record associated with it.
@RestResource(urlMapping='/getAccountOnExternalIdtofetchsinglerecord/*')
global with sharing class getAccounttoSingleRecord {
@Httpget
global static Account fettringchAccount(){
Account obj=new Account();
RestRequest req = RestContext.request;
RestResponse res = Restcontext.response;
string accId =
req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
obj=[Select id , name from Account where id=:accId];
//string ser=JSON.Serialize(obj);
return obj;
}
}
Click "Execute" button and now click "Show Raw Response".
As we can see as the operation of calling this webservice is successfull the HTTP status code returned is 200 by default and now based on requirement of external system if we want to modify the HTTP status code we can do it by using RestResponse class as shown below.
@RestResource(urlMapping='/getAccountOnExternalIdtofetchsinglerecord/*')
global
with sharing class getAccounttoSingleRecord {
@Httpget
global static Account fettringchAccount(){
Account obj=new Account();
RestRequest req = RestContext.request;
RestResponse res = Restcontext.response;
string accId =
req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
obj=[Select id , name from Account where id=:accId];
/***Modify the HTTP status code that will be returned to external
system***/
res.statuscode=201;
/***Modify the HTTP status code that will be returned to external
system***/
return obj;
}
}
Now, Let us try to call the below webservice from workbench.
As we have modified the statuscode to 201 using RestResponse class, we are now able to see the HTTP status code that is returned is 201.
No comments:
Post a Comment