{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":223,"path":"api-reference/cllent-mobile-api-reference/NowAPIServiceiOSAPI.md","title":"NowAPIService class - iOS","bundle":null,"doc_type":null,"release":"australia","body":"\n# NowAPIService class- iOS\n\nThe NowAPIService class provides functions that enable you to perform requests to a specified ServiceNow REST API.\n\n|Name|Type|Description|\n|----|----|-----------|\n|configuration|[NowServiceConfiguration](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/cllent-mobile-api-reference/NowServiceConfigurationiOSStruct.md)|Service configuration to associate with the protocol.|\n\n**Parent Topic:**[Mobile SDK - iOS](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/cllent-mobile-api-reference/MobileSDKiOSAPI.md)\n\n## NowAPIService - data\\(for endpoint: NowAPIEndpoint, queryItems: \\[String: String\\], httpHeaders: \\[String: String\\], body: Data, completion: @escaping \\(Result&lt;DataResponse, NowDataError&gt;\\)\n\nRequests data from the specified ServiceNow REST API and then executes the completion handler.\n\n<table id=\"table_jck_kyp_5pb\" class=\"parameters\"><thead><tr><th>\n\nName\n\n</th><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nfor endpoint\n\n</td><td>\n\n[NowAPIEndpoint](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/cllent-mobile-api-reference/NowAPIEndpointiOSProtocol.md)\n\n</td><td>\n\nObject containing the ServiceNow REST API endpoint to access, including the HTTP method, relative path to the endpoint, path parameters, and whether authentication is required.\n\n</td></tr><tr><td>\n\nqueryItems\n\n</td><td>\n\nArray\n\n</td><td>\n\nOptional. Query parameters for the endpoint as key/value pairs.Default: nil\n\n</td></tr><tr><td>\n\nhttpHeaders\n\n</td><td>\n\nArray\n\n</td><td>\n\nOptional. Request HTTP headers required by the endpoint as key/value pairs. Default: nil\n\n</td></tr><tr><td>\n\nbody\n\n</td><td>\n\nString\n\n</td><td>\n\nOptional. Request body parameters.Default: nil\n\n</td></tr><tr id=\"mobilesdkiOS-completion_data_dataerror-row\"><td>\n\ncompletion\n\n</td><td>\n\n@escaping \\(Result&lt;DataResponse, NowDataError&gt;\\)\n\n</td><td>\n\nCompletion handler to execute after the REST API call completes. Return values for the completion handler:\n\n-   Success: DataResponse that contains the returned data from the REST API call.\n-   Failure: NowDataError\n    -   `accessToken(AccessTokenProviderError)`\n\n        -   **AccessTokenProviderError**: The access token provider's error code or message.\n            -   accessTokenRetrievalFailed\n            -   userSessionError\\(\\_ error: Error\\)\nThrown when there is an error in the access token.\n\n    -   `attachmentValidation`\n\nThrown when an attachment fails validation.\n\n    -   `badResponse(statusCode: HTTPStatusCode)`\n\n        -   **HTTPStatusCode**: Status code received from the instance.\nThrown when a request returns an unexpected response\n\n    -   `cannotDecodeModel(DecodingError)`\n\n        -   **DecodingError**: Decoding error detected.\nThrown when a Codable model cannot be decoded from JSON.\n\n    -   `cannotDecodeProperty(type: Any, from: String)`\n\n        -   **type**: Wrapped type to decode from a string.\n        -   **from**: String to decode to the specified type.\nThrown when a string-wrapped value cannot be decoded from JSON.\n\n    -   `cannotEncodeModel(EncodingError)`\n\n        -   **EncodingError**: Encoding error detected.\nThrown when a Codable model cannot be encoded to JSON.\n\n    -   `cannotParseResponse`\n\nThrown when a response from the instance cannot be parsed into its expected format.\n\n    -   `invalidURL`\n\nThrown when a URL cannot be formed. For example, if the string contains characters that are illegal in a URL or is an empty string.\n\n    -   `missingAttachmentMetadata`\n\nThrown when the attachment metadata header is missing.\n\n    -   `missingServiceConfiguration`\n\nThrown when an expected service configuration is missing.\n\n    -   `missingSysID`\n\nThrown when an expected sys\\_id parameter is missing.\n\n    -   `network(NetworkServiceError)`\n\n        -   genericError\\(String\\)\n        -   operationCanceled\n        -   service​Disabled\n        -   serverError\\(Error\\)\n        -   systemError\\(Error\\)\nThrown when a network service encountered an error.\n\n\n</td></tr></tbody>\n</table>|Type|Description|\n|----|-----------|\n|None| |\n\nThe following code example shows how to call this function.\n\n```\nstruct Endpoint: NowAPIEndpoint {\n  let httpMethod: HTTPMethod\n  let relativePath: String\n  let requiresAuthentication: Bool\n}\n\n/// HTTP request method\npublic enum HTTPMethod: CaseIterable {\n    case options, get, head, post, put, patch, delete, trace, connect\n}\nlet relativePath = “/api/now/account”\nlet endPoint = Endpoint(httpMethod: HTTPMethod.get, relativePath: relativePath, requiresAuthentication: true)\n\napiService.data(for: endPoint, queryItems: [“name”: “abel”], httpHeaders: httpHeaders, body: body.data(using: .utf8)) { [weak self] (result) in\n  switch result {\n    case .success(let response):\n    // Data request successful\n    case .failure(let error):\n    // Data request failed, return NowDataError\n    // .failure(error)\n  }\n}\n\n```\n\n## NowAPIService - data\\(for endpoint: NowAPIEndpoint, queryItems: \\[String: String\\], httpHeaders: \\[String: String\\], body: Data\\) async throws\n\nRequests data from the specified ServiceNow REST API.\n\n<table id=\"table_jck_kyp_5pb\" class=\"parameters\"><thead><tr><th>\n\nName\n\n</th><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nfor endpoint\n\n</td><td>\n\n[NowAPIEndpoint](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/cllent-mobile-api-reference/NowAPIEndpointiOSProtocol.md)\n\n</td><td>\n\nObject containing the ServiceNow REST API endpoint to access, including the HTTP method, relative path to the endpoint, path parameters, and whether authentication is required.\n\n</td></tr><tr><td>\n\nqueryItems\n\n</td><td>\n\nArray\n\n</td><td>\n\nOptional. Query parameters for the endpoint as key/value pairs.Default: nil\n\n</td></tr><tr><td>\n\nhttpHeaders\n\n</td><td>\n\nArray\n\n</td><td>\n\nOptional. Request HTTP headers required by the endpoint as key/value pairs. Default: nil\n\n</td></tr><tr><td>\n\nbody\n\n</td><td>\n\nString\n\n</td><td>\n\nOptional. Request body parameters.Default: nil\n\n</td></tr></tbody>\n</table><table id=\"table_kck_kyp_5pb\" class=\"returns\"><thead><tr><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nDataResponse\n\n</td><td>\n\nReturned when the method is successful. Data from the REST API call.\n\n</td></tr><tr><td>\n\nNowDataError\n\n</td><td>\n\nThrown when the method fails. -   `accessToken(AccessTokenProviderError)`\n\n    -   **AccessTokenProviderError**: The access token provider's error code or message.\n        -   accessTokenRetrievalFailed\n        -   userSessionError\\(\\_ error: Error\\)\nThrown when there is an error in the access token.\n\n-   `attachmentValidation`\n\nThrown when an attachment fails validation.\n\n-   `badResponse(statusCode: HTTPStatusCode)`\n\n    -   **HTTPStatusCode**: Status code received from the instance.\nThrown when a request returns an unexpected response\n\n-   `cannotDecodeModel(DecodingError)`\n\n    -   **DecodingError**: Decoding error detected.\nThrown when a Codable model cannot be decoded from JSON.\n\n-   `cannotDecodeProperty(type: Any, from: String)`\n\n    -   **type**: Wrapped type to decode from a string.\n    -   **from**: String to decode to the specified type.\nThrown when a string-wrapped value cannot be decoded from JSON.\n\n-   `cannotEncodeModel(EncodingError)`\n\n    -   **EncodingError**: Encoding error detected.\nThrown when a Codable model cannot be encoded to JSON.\n\n-   `cannotParseResponse`\n\nThrown when a response from the instance cannot be parsed into its expected format.\n\n-   `invalidURL`\n\nThrown when a URL cannot be formed. For example, if the string contains characters that are illegal in a URL or is an empty string.\n\n-   `missingAttachmentMetadata`\n\nThrown when the attachment metadata header is missing.\n\n-   `missingServiceConfiguration`\n\nThrown when an expected service configuration is missing.\n\n-   `missingSysID`\n\nThrown when an expected sys\\_id parameter is missing.\n\n-   `network(NetworkServiceError)`\n\n    -   genericError\\(String\\)\n    -   operationCanceled\n    -   service​Disabled\n    -   serverError\\(Error\\)\n    -   systemError\\(Error\\)\nThrown when a network service encountered an error.\n\n\n</td></tr></tbody>\n</table>The following code examples shows how to call this method.\n\n```\ndo {\n    let dataResponse = try await apiService.data(for: endpoint, queryItems: queryItems, httpHeaders: httpHeaders, body: body)\n    // Data request successful\n} catch {\n    // Data request failed, NowDataError thrown\n}\n```\n\n## NowAPIService - data\\(for endpoint: NowAPIEndpoint, queryItems: \\[String: String\\]? = nil, httpHeaders: \\[String: String\\]? = nil, body: Data? = nil\\)\n\nRequests data from the specified ServiceNow REST API.\n\n**Note:** This method has been deprecated. You should use the async/await implementation of the method instead.\n\n<table id=\"table_sfb_5tp_5pb\" class=\"parameters\"><thead><tr><th>\n\nName\n\n</th><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nfor endpoint\n\n</td><td>\n\n[NowAPIEndpoint](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/cllent-mobile-api-reference/NowAPIEndpointiOSProtocol.md)\n\n</td><td>\n\nObject containing the ServiceNow REST API endpoint to access, including the HTTP method, relative path to the endpoint, path parameters, and whether authentication is required.\n\n</td></tr><tr><td>\n\nqueryItems\n\n</td><td>\n\nArray\n\n</td><td>\n\nOptional. Query parameters for the endpoint as key/value pairs.Default: nil\n\n</td></tr><tr><td>\n\nhttpHeaders\n\n</td><td>\n\nArray\n\n</td><td>\n\nOptional. Request HTTP headers required by the endpoint as key/value pairs.Default: nil\n\n</td></tr><tr><td>\n\nbody\n\n</td><td>\n\nData\n\n</td><td>\n\nOptional. Request body parameters. Default: nil\n\n</td></tr></tbody>\n</table><table id=\"table_tfb_5tp_5pb\" class=\"returns\"><thead><tr><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nAnyPublisher&lt;DataResponse, NowDataError&gt;\n\n</td><td>\n\nSuccess: Data returned by the REST API.Failure: NowDataError\n\n-   `accessToken(AccessTokenProviderError)`\n\n    -   **AccessTokenProviderError**: The access token provider's error code or message.\n        -   accessTokenRetrievalFailed\n        -   userSessionError\\(\\_ error: Error\\)\nThrown when there is an error in the access token.\n\n-   `attachmentValidation`\n\nThrown when an attachment fails validation.\n\n-   `badResponse(statusCode: HTTPStatusCode)`\n\n    -   **HTTPStatusCode**: Status code received from the instance.\nThrown when a request returns an unexpected response\n\n-   `cannotDecodeModel(DecodingError)`\n\n    -   **DecodingError**: Decoding error detected.\nThrown when a Codable model cannot be decoded from JSON.\n\n-   `cannotDecodeProperty(type: Any, from: String)`\n\n    -   **type**: Wrapped type to decode from a string.\n    -   **from**: String to decode to the specified type.\nThrown when a string-wrapped value cannot be decoded from JSON.\n\n-   `cannotEncodeModel(EncodingError)`\n\n    -   **EncodingError**: Encoding error detected.\nThrown when a Codable model cannot be encoded to JSON.\n\n-   `cannotParseResponse`\n\nThrown when a response from the instance cannot be parsed into its expected format.\n\n-   `invalidURL`\n\nThrown when a URL cannot be formed. For example, if the string contains characters that are illegal in a URL or is an empty string.\n\n-   `missingAttachmentMetadata`\n\nThrown when the attachment metadata header is missing.\n\n-   `missingServiceConfiguration`\n\nThrown when an expected service configuration is missing.\n\n-   `missingSysID`\n\nThrown when an expected sys\\_id parameter is missing.\n\n-   `network(NetworkServiceError)`\n\n    -   genericError\\(String\\)\n    -   operationCanceled\n    -   service​Disabled\n    -   serverError\\(Error\\)\n    -   systemError\\(Error\\)\nThrown when a network service encountered an error.\n\n\n</td></tr></tbody>\n</table>The following code example shows how to call this function.\n\n```\nstruct Endpoint: NowAPIEndpoint {\n    let httpMethod: HTTPMethod\n    let relativePath: String\n    let requiresAuthentication: Bool\n}\n\n/// HTTP request method\npublic enum HTTPMethod: CaseIterable {\n    case options, get, head, post, put, patch, delete, trace, connect\n}\nlet relativePath = \"/api/now/account\"\nlet endPoint = Endpoint(httpMethod: HTTPMethod.get, relativePath: relativePath, requiresAuthentication: true)\n\n// Using Combine\napiService.data(for: Endpoint(httpMethod: httpMethod, relativePath: endpointPath, requiresAuthentication: requiresAuthentication), queryItems: queryItems, httpHeaders: httpHeaders, body: body.data(using: .utf8))\n  .receive(on: DispatchQueue.main)\n  .sink { [weak self] (comp) in\n    if case let .failure(error) = comp {\n      // Data request failed, return NowDataError\n    }\n  } receiveValue: { [weak self] (response) in\n    // Data request successful\n    self?.publish(data: response.data)\n  }\n  .store(in: &self.subscriptions)\n}\n```\n\n"}}