either The URL to request from or HttpsRequestOptions options.
-Downloads the content from the specified URL as binary and returns an ArrayBuffer.
+either The URL to request from or HttpsRequestOptions options.
+Downloads the content from the specified URL and attempts to save it as file.
-either The URL to request from or HttpsRequestOptions options.
-Optional destinationFilePath: stringOptional. The downloaded file path.
-Downloads the content from the specified URL and attempts to save it as file.
+either The URL to request from or HttpsRequestOptions options.
+OptionaldestinationFilePath: stringOptional. The downloaded file path.
+Downloads the content from the specified URL and attempts to decode it as an image.
-either The URL to request from or HttpsRequestOptions options.
-Downloads the content from the specified URL and attempts to decode it as an image.
+either The URL to request from or HttpsRequestOptions options.
+Downloads the content from the specified URL as a string and returns its JSON.parse representation.
-either The URL to request from or HttpsRequestOptions options.
-Downloads the content from the specified URL as a string and returns its JSON.parse representation.
+either The URL to request from or HttpsRequestOptions options.
+Downloads the content from the specified URL as a string.
-either The URL to request from or HttpsRequestOptions options.
-Downloads the content from the specified URL as a string.
+either The URL to request from or HttpsRequestOptions options.
+Optional useLegacy: UOptionaluseLegacy: UOptional options: CacheOptionsOptionaloptions: CacheOptionsNativescript plugin for https requests
-* [Installation](#installation)
* [A drop-in replacement for the [default http module](https://docs.nativescript.org/cookbook/http).](#a-drop-in-replacement-for-the-default-http-modulehttpsdocsnativescriptorgcookbookhttp)
* [Features](#features)
* [FAQ](#faq)
* [Installation](#installation-1)
* [Examples](#examples)
* [Hitting an API using `GET` method](#hitting-an-api-using-get-method)
* [Configuration](#configuration)
* [Installing your SSL certificate](#installing-your-ssl-certificate)
* [Enabling SSL pinning](#enabling-ssl-pinning)
* [Disabling SSL pinning](#disabling-ssl-pinning)
* [useLegacy](#uselegacy)
* [Cookie](#cookie)
* [Enabling Cache](#enabling-cache)
* [Multipart form data](#multipart-form-data)
* [Options](#options)
* [Webpack / bundling](#webpack--bundling)
* [`iOS` Troubleshooting](#ios-troubleshooting)
* [`Android` troubleshooting](#android-troubleshooting)
-
+
+
+* [Installation](#installation)
+ * [A drop-in replacement for the [default http module](https://docs.nativescript.org/cookbook/http).](#a-drop-in-replacement-for-the-default-http-modulehttpsdocsnativescriptorgcookbookhttp)
+* [Features](#features)
+* [FAQ](#faq)
+* [Installation](#installation-1)
+* [Examples](#examples)
+ * [Hitting an API using `GET` method](#hitting-an-api-using-get-method)
+* [Configuration](#configuration)
+ * [Installing your SSL certificate](#installing-your-ssl-certificate)
+ * [Enabling SSL pinning](#enabling-ssl-pinning)
+ * [Disabling SSL pinning](#disabling-ssl-pinning)
+ * [useLegacy](#uselegacy)
+ * [Cookie](#cookie)
+ * [Enabling Cache](#enabling-cache)
+ * [Multipart form data](#multipart-form-data)
+ * [Options](#options)
+* [Webpack / bundling](#webpack--bundling)
+* [`iOS` Troubleshooting](#ios-troubleshooting)
+* [`Android` troubleshooting](#android-troubleshooting)
+
+Run the following command from the root of your project:
+ + +Run the following command from the root of your project:
ns plugin add @nativescript-community/https
Easily integrate the most reliable native networking libraries with the latest and greatest HTTPS security features.
@@ -69,9 +85,9 @@@nativescript-community/https
-Plugin version 2.0.0 bumps
AFNetworkingon iOS to 4.0.0 which no longer relies onUIWebView. Make sure to runpod repo updateto get the latestAFNetworkingpod on your development machine.A drop-in replacement for the default http module.
- -Features
+
A drop-in replacement for the default http module.
+ +Features
- - -
- Modern TLS & SSL security features
- Shared connection pooling reduces request latency
- Silently recovers from common connection problems
@@ -82,50 +98,57 @@@nativescript-community/https
- Cache
- Basic Cookie support
FAQ
+ + +FAQ
What the flip is SSL pinning and all this security mumbo jumbo?
How to make your apps more secure with SSL pinning.
-Do I have to use SSL pinning?
No. This plugin works out of the box without any security configurations needed. Either way you'll still benefit from all the features listed above.
- - -Installation
- - -tns plugin add @nativescript-community/https -Examples
Hitting an API using
GETmethod- - -import * as Https from '@nativescript-community/https';
Https.request({
url: 'https://httpbin.org/get',
method: 'GET',
timeout: 30, // seconds (default 10)
})
.then(function (response) {
console.log('Https.request response', response);
})
.catch(function (error) {
console.error('Https.request error', error);
}); -Configuration
Installing your SSL certificate
Create a folder called
+assetsin your projectsappfolder like so<project>/app/assets. Using chrome, go to the URL where the SSL certificate resides. View the details then drag and drop the certificate image into theassetsfolder.No. This plugin works out of the box without any security configurations needed. Either way you'll still benefit from all the features listed above.
+ + +Installation
+ + + +tns plugin add @nativescript-community/https +Examples
Hitting an API using
GETmethod+ + + +import * as Https from '@nativescript-community/https';
Https.request({
url: 'https://httpbin.org/get',
method: 'GET',
timeout: 30, // seconds (default 10)
})
.then(function (response) {
console.log('Https.request response', response);
})
.catch(function (error) {
console.error('Https.request error', error);
}); +Configuration
Installing your SSL certificate
Create a folder called
assetsin your projectsappfolder like so<project>/app/assets. Using chrome, go to the URL where the SSL certificate resides. View the details then drag and drop the certificate image into theassetsfolder.-
Enabling SSL pinning
-import { knownFolders } from 'file-system';
import * as Https from '@nativescript-community/https';
let dir = knownFolders.currentApp().getFolder('assets');
let certificate = dir.getFile('httpbin.org.cer').path;
Https.enableSSLPinning({ host: 'httpbin.org', certificate }); -Once you've enabled SSL pinning you CAN NOT re-enable with a different
-hostorcertificatefile.Disabling SSL pinning
+import * as Https from '@nativescript-community/https';
Https.disableSSLPinning(); -Enabling SSL pinning
+ +import { knownFolders } from 'file-system';
import * as Https from '@nativescript-community/https';
let dir = knownFolders.currentApp().getFolder('assets');
let certificate = dir.getFile('httpbin.org.cer').path;
Https.enableSSLPinning({ host: 'httpbin.org', certificate }); +Once you've enabled SSL pinning you CAN NOT re-enable with a different
+hostorcertificatefile.Disabling SSL pinning
+import * as Https from '@nativescript-community/https';
Https.disableSSLPinning(); +All requests after calling this method will no longer utilize SSL pinning until it is re-enabled once again.
-useLegacy
There is a new option called
useLegacy. You can set of every request options. +useLegacy
There is a new option called
useLegacy. You can set of every request options. When using that option the request will behave more like {N} http module.-
- the
contentreturned by a request is not the resulting string but an object. It follows HTTPContent format for the most part. You can calltoJSONortoFile. The only difference is thattoFilereturns aPromise<File>which means that it is async and run in a background thread!- an error return a
contenttoo allowing you to read its content.Cookie
By default basic Cookie support is enabled to work like in {N}
httpmodule. +Cookie
By default basic Cookie support is enabled to work like in {N}
-httpmodule. In the future more options will be addedEnabling Cache
-import { knownFolders, path } from '@nativescript/core/file-system';
import * as Https from '@nativescript-community/https';
Https.setCache({
diskLocation: path.join(knownFolders.documents().path, 'httpcache'),
diskSize: 10 * 1024 * 1024, // 10 MiB
});
/// later on when calling your request you can use the cachePolicy option -Multipart form data
If you set the
-Content-Typeheader to"multipart/form-data"the request body will be evaluated as a multipart form data. Each body parameter is expected to be in this format:+{
data: any
parameterName: string,
fileName?: string
contentType?: string
}
-Enabling Cache
+ +import { knownFolders, path } from '@nativescript/core/file-system';
import * as Https from '@nativescript-community/https';
Https.setCache({
diskLocation: path.join(knownFolders.documents().path, 'httpcache'),
diskSize: 10 * 1024 * 1024, // 10 MiB
});
/// later on when calling your request you can use the cachePolicy option +Multipart form data
If you set the
+Content-Typeheader to"multipart/form-data"the request body will be evaluated as a multipart form data. Each body parameter is expected to be in this format:+{
data: any
parameterName: string,
fileName?: string
contentType?: string
}
+if
-fileNameandcontentTypeare set then data is expected to be either aNSDataon iOS or anative.Array<number>on Android.Options
+export interface HttpsSSLPinningOptions {
host: string;
certificate: string;
allowInvalidCertificates?: boolean;
validatesDomainName?: boolean;
commonName?: string;
}
import { HttpRequestOptions } from 'tns-core-modules/http';
export interface HttpsRequestOptions extends HTTPOptions {
useLegacy?: boolean;
cachePolicy?: 'noCache' | 'onlyCache' | 'ignoreCache';
onProgress?: (current: number, total: number) => void;
} -Options
+export interface HttpsSSLPinningOptions {
host: string;
certificate: string;
allowInvalidCertificates?: boolean;
validatesDomainName?: boolean;
commonName?: string;
}
import { HttpRequestOptions } from 'tns-core-modules/http';
export interface HttpsRequestOptions extends HTTPOptions {
useLegacy?: boolean;
cachePolicy?: 'noCache' | 'onlyCache' | 'ignoreCache';
onProgress?: (current: number, total: number) => void;
} ++ +
@@ -133,7 +156,8 @@ -@nativescript-community/https
Description + +@@ -147,13 +171,14 @@ host: stringThis must be the request domain name eg sales.company.org.@nativescript-community/https
- allowInvalidCertificates?: booleanDefault: +false. This should always befalseif you are using SSL pinning. Set this totrueif you're using a self-signed certificate.Default: false. This should always befalseif you are using SSL pinning. Set this totrueif you're using a self-signed certificate.- validatesDomainName?: booleanDefault: true. Determines if the domain name should be validated with your pinned certificate.- - -
@@ -161,48 +186,56 @@ -@nativescript-community/https
Description + +useLegacy?: booleanDefault: false. [IOS only] set to true in order to get the response data (when status >= 300)in thecontentdirectly instead ofresponse.body.content.- `cachePolicy?: 'noCache' -'onlyCache' +`cachePolicy?: 'noCache' +'onlyCache' - onProgress?: (current: number, total: number) => void[IOS only] Set the progress callback. Webpack / bundling
Since you're probably shipping a certificate with your app (like our demo does), -make sure it's bundled by Webpack as well. You can do this by adding the certificate(s) with the
- - -CopyWebpackPlugin.
iOSTroubleshooting--Please educate yourself on iOS's App Transport Security before starting beef!
If you try and hit an
+ +httpsroute without adding it to App Transport Security's whitelist it will not work! + + +Webpack / bundling
Since you're probably shipping a certificate with your app (like our demo does), +make sure it's bundled by Webpack as well. You can do this by adding the certificate(s) with the
+ + +CopyWebpackPlugin.
iOSTroubleshooting++Please educate yourself on iOS's App Transport Security before starting beef!
If you try and hit an
-httpsroute without adding it to App Transport Security's whitelist it will not work! You can bypass this behavior by adding the following to your projectsInfo.plist:+<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict> -+NSAppTransportSecurity ++ +NSAllowsArbitraryLoads ++ - - -This plugin does not add
NSAllowsArbitraryLoadsto your projectsInfo.plistfor you.
AndroidtroubleshootingIf you app crashes with a message that it's doing too much networking on the main thread, +
+ +
AndroidtroubleshootingIf you app crashes with a message that it's doing too much networking on the main thread, then pass the option
- - -allowLargeResponsewith valuetrueto therequestfunction.Thanks
+ + +
+Thanks
-
- Who Why + +@@ -222,98 +255,118 @@ Robert Laverty For creating and maintaining this plugin for a long time @nativescript-community/https
Square okhttp An HTTP+HTTP/2 client for Android and Java applications. -Examples:
-
- Basic
+ +
Examples:
+
- - -- Basic +
- A basic example
Demos and Development
Repo Setup
The repo uses submodules. If you did not clone with
---recursivethen you need to callgit submodule update --init + + +Demos and Development
Repo Setup
The repo uses submodules. If you did not clone with
+--recursivethen you need to call+git submodule update --initThe package manager used to install and link dependencies must be
pnpmoryarn.npmwont work.To develop and test: if you use
yarnthen runyarnif you usepnpmthen runpnpm iInteractive Menu:
To start the interactive menu, run
-npm start(oryarn startorpnpm start). This will list all of the commonly used scripts.Build
+npm run build.all -Build
+npm run build.all +WARNING: it seems
-yarn build.allwont always work (not finding binaries innode_modules/.bin) which is why the doc explicitly usesnpm runDemos
+npm run demo.[ng|react|svelte|vue].[ios|android]
npm run demo.svelte.ios # Example -Demos
+npm run demo.[ng|react|svelte|vue].[ios|android]
npm run demo.svelte.ios # Example +Demo setup is a bit special in the sense that if you want to modify/add demos you dont work directly in
- - -demo-[ng|react|svelte|vue]Instead you work indemo-snippets/[ng|react|svelte|vue]-You can start from theinstall.tsof each flavor to see how to register new demosContributing
Update repo
You can update the repo files quite easily
+You can start from theinstall.tsof each flavor to see how to register new demos + + +Contributing
Update repo
You can update the repo files quite easily
First update the submodules
-+npm run update -+npm run update +Then commit the changes Then update common files
-+npm run sync -+npm run sync +Then you can run
-yarn|pnpm, commit changed files if anyUpdate readme
-npm run readme -Update doc
-npm run doc -Publish
The publishing is completely handled by
-lerna(you can add-- --bump majorto force a major release) -Simply run-npm run publish -modifying submodules
The repo uses https:// for submodules which means you won't be able to push directly into the submodules. +
Update readme
+ +npm run readme +Update doc
+ +npm run doc +Publish
The publishing is completely handled by
+lerna(you can add-- --bump majorto force a major release) +Simply run+ +npm run publish +modifying submodules
The repo uses https:// for submodules which means you won't be able to push directly into the submodules. One easy solution is t modify
-~/.gitconfigand add[url "ssh://git@github.com/"]
pushInsteadOf = https://github.com/ +- - -[url "ssh://git@github.com/"]
pushInsteadOf = https://github.com/Questions
If you have any questions/issues/comments please feel free to create an issue or start a conversation in the NativeScript Community Discord.
- -Demos and Development
Repo Setup
The repo uses submodules. If you did not clone with
---recursivethen you need to callgit submodule update --init + + + +Questions
If you have any questions/issues/comments please feel free to create an issue or start a conversation in the NativeScript Community Discord.
+ +Demos and Development
Repo Setup
The repo uses submodules. If you did not clone with
+--recursivethen you need to call+git submodule update --initThe package manager used to install and link dependencies must be
pnpmoryarn.npmwont work.To develop and test: if you use
yarnthen runyarnif you usepnpmthen runpnpm iInteractive Menu:
To start the interactive menu, run
-npm start(oryarn startorpnpm start). This will list all of the commonly used scripts.Build
+npm run build.all -Build
+npm run build.all +WARNING: it seems
-yarn build.allwont always work (not finding binaries innode_modules/.bin) which is why the doc explicitly usesnpm runDemos
+npm run demo.[ng|react|svelte|vue].[ios|android]
npm run demo.svelte.ios # Example -Demos
+npm run demo.[ng|react|svelte|vue].[ios|android]
npm run demo.svelte.ios # Example +Demo setup is a bit special in the sense that if you want to modify/add demos you dont work directly in
- -demo-[ng|react|svelte|vue]Instead you work indemo-snippets/[ng|react|svelte|vue]-You can start from theinstall.tsof each flavor to see how to register new demosContributing
Update repo
You can update the repo files quite easily
+You can start from theinstall.tsof each flavor to see how to register new demos + +Contributing
Update repo
You can update the repo files quite easily
First update the submodules
-+npm run update -+npm run update +Then commit the changes Then update common files
-+npm run sync -+npm run sync +Then you can run
-yarn|pnpm, commit changed files if anyUpdate readme
-npm run readme -Update doc
-npm run doc -Publish
The publishing is completely handled by
-lerna(you can add-- --bump majorto force a major release) -Simply run-npm run publish -modifying submodules
The repo uses https:// for submodules which means you won't be able to push directly into the submodules. +
Update readme
+ +npm run readme +Update doc
+ +npm run doc +Publish
The publishing is completely handled by
+lerna(you can add-- --bump majorto force a major release) +Simply run+ +npm run publish +modifying submodules
The repo uses https:// for submodules which means you won't be able to push directly into the submodules. One easy solution is t modify
-~/.gitconfigand add[url "ssh://git@github.com/"]
pushInsteadOf = https://github.com/ +- -[url "ssh://git@github.com/"]
pushInsteadOf = https://github.com/Questions
If you have any questions/issues/comments please feel free to create an issue or start a conversation in the NativeScript Community Discord.
-
If you have any questions/issues/comments please feel free to create an issue or start a conversation in the NativeScript Community Discord.
+Optional forceOptional memoryOptional contentOptional fileOptional allowOn Android large responses may crash the app (fi. https://httpbin.org/bytes/10000). +
OptionalallowOn Android large responses may crash the app (fi. https://httpbin.org/bytes/10000). By setting this to true and when not using useLegacy, we allow large responses on the main thread (which this plugin currently does). Note that once set to true, this policy remains active until the app is killed.
-Optional bodyOptional cacheOptional contentcontent can be used to pass native custom okhttp3.RequestBody
-Optional cookiesdefault to true. Android and iOS only store cookies in memory! it will be cleared after an app restart
-Optional headersOptional oniOS for now
-Optional paramsOptional responsedefault to true. Put to false to run response callback on current thread
-Optional tagOptional timeoutDefault 10 (seconds).
-OptionalbodyOptionalcacheOptionalcontentcontent can be used to pass native custom okhttp3.RequestBody
+Optionalcookiesdefault to true. Android and iOS only store cookies in memory! it will be cleared after an app restart
+OptionalheadersGets or sets the request headers in JSON format.
+Gets or sets the request method.
+OptionaloniOS for now
+OptionalparamsOptionalprogressOptionalresponsedefault to true. Put to false to run response callback on current thread
+OptionaltagOptionaltimeoutDefault 10 (seconds).
+Gets or sets the request url.
+Optional contentOptional descriptionOptional failureOptional headersOptional reasonOptional responseOptional statusOptional urlOptional allowOptional commonOptional validatesConst ConstConst Const
Downloads the content from the specified URL as binary and returns an ArrayBuffer.
-