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.

    1Task {
    2    let request = RestRequest(
    3        method: .GET,
    4        serviceHostType: .instance,
    5        path: "/v42.0/chatter/feeds/user-profile/me/feed-elements",
    6        queryParams: nil
    7    )
    8
    9    do {
    10        let response = try await RestClient.shared.send(request: request)
    11        // Process the response
    12    } catch let error as RestClientError {
    13        SalesforceLogger.d(RootViewController.self,
    14            message: "Error invoking: \(request), \(error)")
    15    } catch {
    16        SalesforceLogger.d(RootViewController.self,
    17            message: "Unexpected error invoking: \(request),
    18            \(error.localizedDescription)")
    19    }
    20}
  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.

    1if let dataString = "{\"body\" : {\"messageSegments\" : [{\"type\" : \"Text\", " +
    2 "\"text\" : \"Some Comment\"}]}, \"feedElementType\":\"FeedItem\", " +
    3 "\"subjectId\":\"me\"}" if let data = dataString.data(using: .utf8) {
    4    let request = RestRequest(
    5        method: .POST,
    6        serviceHostType: .instance,
    7        path: "/v42.0/chatter/feed-elements",
    8        queryParams: nil
    9    )
    10    request.setCustomRequestBodyData(data, contentType: "application/json")
    11
    12
    13    Task {
    14        do {
    15            let response = try await RestClient.shared.send(request: request)
    16            // Process the response
    17        } catch let error as RestClientError {
    18            SalesforceLogger.d(RootViewController.self,
    19                message: "Error invoking: \(request), \(error)")
    20        } catch {
    21            SalesforceLogger.d(RootViewController.self,
    22                message: "Unexpected error invoking: \(request),
    23                \(error.localizedDescription)")
    24        }
    25    }
    26}
  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")

We've Moved

Welcome to the new home of the Mobile SDK Developer Guide! For now, the Japanese guide can be found in PDF form.