Note: This release is in preview. Features described here don’t become generally available until the latest general availability date that Salesforce announces for this release. Before then, and where features are noted as beta, pilot, or developer preview, we can’t guarantee general availability within any particular time frame or at all. Make your purchase decisions only on the basis of generally available products and features.

Using REST Request Methods

Salesforce offers some product-specific REST APIs that have no relationship to Salesforce Platform APIs. You can use Mobile SDK resources to configure and send such requests. For these cases, you create and configure your RestRequest object directly, instead of relying on factory methods.

To send a non-SOQL and non-SOSL REST request using RestClient:

  1. Create an instance of RestRequest.
  2. Set the properties you need on the RestRequest object.
  3. Call send on the RestClient object, passing in the RestRequest object you created as the first parameter.

    The following example performs a GET operation to obtain all items in a specific Chatter feed.

    1let request = RestRequest(method: .GET, serviceHostType: .instance,
    2    path: "/v42.0/chatter/feeds/user-profile/me/feed-elements", 
    3    queryParams: nil)
    4RestClient.shared.send(request: request { [weak self] (result) in
    5    switch result {
    6        case .success(let response):
    7            // do something with response
    8        case .failure(let error):
    9            SalesforceLogger.d(RootViewController.self, message:"Error invoking: \(request) , \(error)")
    10    } 
    11}
  4. To perform a request with parameters, wrap your parameter string using SFJsonUtils.object(fromJSONString:), and assign it to the queryParams property of RestRequest. To send a custom request, create a Dictionary object and use the setCustomRequestBodyData(_:contentType:) method of RestRequest.

    The following example uses a custom request body to add a comment to a Chatter feed.

    1let dataString = "{\"body\" : {\"messageSegments\" : [{\"type\" : \"Text\", " +
    2    "\"text\" : \"Some Comment\"}]}, \"feedElementType\":\"FeedItem\", " +
    3    "\"subjectId\":\"me\"}"
    4
    5if let data = dataString.data(using: .utf8) {
    6    
    7    let request = RestRequest(method: .POST, serviceHostType: .instance, 
    8        path: "/v42.0/chatter/feed-elements", queryParams: nil) 
    9    request.setCustomRequestBodyData(data, contentType: "application/json")
    10    
    11    RestClient.shared.send(request: request { [weak self] (result) in
    12        switch result {
    13            case .success(let response):
    14                // do something with response
    15            case .failure(let error):
    16                SalesforceLogger.d(RootViewController.self, message:"Error invoking: \(request) , \(error)")
    17        } 
    18    }
    19}
  5. To set an HTTP header for your request, use the setHeaderValue(_:forHeaderName:) method. This method can help you when you’re displaying Chatter feeds, which come pre-encoded for HTML display. To avoid displaying unwanted escape sequences in Chatter comments, set the X-Chatter-Entity-Encoding header of your request to “false”:
    1...
    2request.setHeaderValue("false", forHeaderName:"X-Chatter-Entity-Encoding")