SFDC Apex Call Rest

Posted by ExiaHuang on April 21, 2017

SFDC Apex Call Rest

Use apex code to call Rest Api

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
	RestRequest req = new RestRequest();
	req.addHeader('Content-Type', 'application/json; charset=UTF-8');
	req.requestURI = '/your_url';
	req.httpMethod = 'POST';
	req.requestBody = Blob.valueOf(JSON.serialize(new Map<String, Object>{
	    'param1' => '12345',
	    'param2' => '3456',
	    'param3'      => new List<Map<String, Object>> {
	      new Map<String, Object> {
	        'p1'     => '2',
	        'p2'    => 1
	      }
	  }
	}));

	RestContext.request = req;
	RestContext.response = new RestResponse();

	YourClass.CallPostRest();

	String body = RestContext.response.responseBody.toString();
	System.debug(body);

Call Use Rest Api

call the url /services/apexrest/your_url by SalesforceXyTools For Chrome { “param1”: “1”, “param2”: “2”, “param3”: { “p1”: “3”, “p2”: 1 } }