HTTP requests (Fetch API)
Version compatibility
This feature was introduced in version 4.6 of the JourneyApps Container.
HTTP on iOS disabled from v4.27
Note: In 2016 Apple announced a security feature for iOS called Application Transport Security (ATS). This feature will eventually enforce HTTPS for all network requests. Although the JourneyApps Container already uses HTTPS for all communications with the JourneyApps Backend, it was still possible for a developer to use the fetch()
function to create insecure HTTP requests.
We've activated ATS on our iOS Containers from v4.27, disabling the ability to create HTTP requests.
The Fetch API allows apps to make HTTP Requests to external web services directly from the user's device. This is especially useful for use cases involving "live lookups" on external web services which are required as part of the app workflow. An example would be doing a credit check in the app before allowing the user to continue in issuing a quote for a financial product.
The Fetch API implementation is Promise based. It is based on the WHAT-WG Fetch API specification.
All outgoing requests are subject to Cross Origin Resource Sharing (CORS) restrictions.
Furthermore, there are two important caveats:
The Promise returned from
fetch()
won't reject on an HTTP error status even if the response is an HTTP 404 or 500. Instead, it will resolve normally, and it will only reject on a network failure, or if anything prevented the request from completing.By default,
fetch()
won't send any cookies to the server, resulting in unauthenticated requests if the site relies on maintaining a user session.
Usage
Here are examples of how to use the fetch() in an app:
GET
Here is an example of a GET request where the response in displayed in a dialog:
POST
Here is an example of a simple POST where the response in displayed in a dialog:
Basic Authentication
fetch.Auth
provides helper functions for basic
and token
authorization headers. For example, the basic
helper correctly format encodes the username and password into a valid Authorization
header:
Network errors
As mentioned before, the fetch
promise will be rejected only for network errors. Use .caught
(compatibility alias for .catch
) or .then(null, handler)
HTTP errors
The code can check for 2xx HTTP response codes by using response.ok
. It is recommended to do this in every response handler.
Further Reading
Last updated