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:
-
Create an instance of
RestRequest. -
Set the properties you need on the
RestRequestobject. -
Call
sendon theRestClientobject, passing in theRestRequestobject you created as the first parameter.The following example performs a GET operation to obtain all items in a specific Chatter feed.
Task { let request = RestRequest( method: .GET, serviceHostType: .instance, path: "/v42.0/chatter/feeds/user-profile/me/feed-elements", queryParams: nil )
do {
let response = try await RestClient.shared.send(request: request)
// Process the response
} catch let error as RestClientError {
SalesforceLogger.d(RootViewController.self,
message: "Error invoking: \(request), \(error)")
} catch {
SalesforceLogger.d(RootViewController.self,
message: "Unexpected error invoking: \(request),
\(error.localizedDescription)")
}
}
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.
```bash
if let dataString = "{\"body\" : {\"messageSegments\" : [{\"type\" : \"Text\", " +
"\"text\" : \"Some Comment\"}]}, \"feedElementType\":\"FeedItem\", " +
"\"subjectId\":\"me\"}" if let data = dataString.data(using: .utf8) {
let request = RestRequest(
method: .POST,
serviceHostType: .instance,
path: "/v42.0/chatter/feed-elements",
queryParams: nil
)
request.setCustomRequestBodyData(data, contentType: "application/json")
Task {
do {
let response = try await RestClient.shared.send(request: request)
// Process the response
} catch let error as RestClientError {
SalesforceLogger.d(RootViewController.self,
message: "Error invoking: \(request), \(error)")
} catch {
SalesforceLogger.d(RootViewController.self,
message: "Unexpected error invoking: \(request),
\(error.localizedDescription)")
}
}
}
-
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 theX-Chatter-Entity-Encodingheader of your request to “false”:... request.setHeaderValue("false", forHeaderName:"X-Chatter-Entity-Encoding")