Load data from a server
Use Angular's HttpClient and httpResource the way you would on the web. Interceptors, timeouts and canceling work too.
Set up HttpClient
Add provideHttpClient() to your app config:
Load data with httpResource
httpResource sends the request when the component is created, and again whenever a signal the URL function reads changes.
Send data with HttpClient
Angular's HTTP guide covers the rest: headers, query parameters, interceptors and error handling.
How requests reach the network
Lynx can make network requests only from the background thread, and your app runs on the main thread. So AngularLynx gives the main thread its own fetch and XMLHttpRequest. They pass each request to the background thread, which sends it, and bring the response back.
- Both of Angular's backends work.
provideHttpClient()usesfetch, andprovideHttpClient(withXhr())usesXMLHttpRequest. - Libraries that call
fetchorXMLHttpRequestwork too, such as axios or a GraphQL client. - In the web preview, requests use the browser's own
fetch.
What's different on a device
- Use absolute URLs. A Lynx app has no page URL, so there's nothing to resolve
/api/postsagainst, and the request fails. Put your server's address in a constant, or add it in an interceptor. - Use HTTPS. The apps
ng build --platformmakes block plainhttp://URLs, except to the device itself (localhostand127.0.0.1). Lynx Explorer allows them for development, and so does the Android development buildng serve --platform androidmakes, but the app you ship doesn't. - There's no CORS. A device isn't a browser, so a server doesn't need
Access-Control-Allow-Originheaders. The web preview is a browser, so it still applies there. - Send strings, JSON,
URLSearchParams,ArrayBuffers, typed arrays orBlobs. ABlobis sent with its type as theContent-Type, unless you set one, so a photo fromLynxImagePickeruploads as it is. Lynx has noFormData, so send the file itself rather than a form.responseType: 'blob'works too. - Calling Lynx's own
fetchin abackgroundFn? SpellContent-Typeexactly. Up to Lynx 4.2, header names there are case-sensitive: a request that setscontent-typegets a second,text/plainone, and the server may read either.response.headers.get()is case-sensitive too, and a response's header names come lowercase on Android but capitalized on iOS. ThefetchandHttpClientyour components use take care of this. - Responses arrive whole.
response.bodyis aReadableStream, but it gives you the whole body in one chunk once the response has ended. So a response that stays open, such as server-sent events, never arrives. Firebase Firestore detects this and switches to long polling by itself. - Progress arrives once. With
reportProgress, the download (and, withwithXhr(), the upload) reports its progress once, when it's complete. - A request that never reaches the server fails with status 0, as on the web. The error says why, for example "Could not connect to the server."
- Don't rely on
statusText. On iOS, Lynx reportsOKfor every status. Checkstatusinstead. - Cookies belong to the host app.
withCredentialshas no effect: the host app's networking decides which cookies are sent.
The host app has to provide Lynx's HTTP service. Lynx Explorer and the apps ng build --platform makes have it. If you embed your app in a native app of your own, register it there, as Lynx's integration guide shows.
Test it
provideHttpClientTesting() works as in any Angular app. Add it with provideHttpClient() to the providers of render(), then answer requests with HttpTestingController.
Related
- The JavaScript environment: what else Lynx adds and lacks
- Threads: why requests go through the background thread
