Http
The HTTP module provides a functionality, which allows submitting GET and POST requests for both platforms (iOS and Android). With its methods, we could request a data from a remote server and to represent the received response in different formats. We could request or send data with the following ways:
getString
getJSON
getImage
getFile
request
Usage
HTTP Get
The example demonstrates different ways, how we could receive content from a server while making HTTP GET request.
The getString
method allows us to make a request and to get the response body as a string value.
The getJSON
give us a simple way to get the response body as JSON object and to access the data while using all benefits of the JSON.
getImage
allow us to get an image from a specific URL. The returned object will be ImageSource and it could be used for direct displaying the source into Image view.
With request
method we can make a request and check the response status code by accessing statusCode
property.
The example demonstrates, how to get the request-response header and how to access the available data in it.
The example demonstrates, how to get the request-response content and how to represent the received data as a String
value or JSON
object. We could also use toImage
method when we download an image.
The example demonstrates how to download a file while using getFile
method.
Note: By default the file will be saved in Documents folder.
In the getFile
method we could also specify the path, where the file to be saved. This scenario is demonstrated in the example below, where the image file will be kept in the current application folder.
HTTP Post
The example demonstrates, how to make HTTP POST request and how to get request response.
Methods
Name | Type | Description |
---|---|---|
getFile(url: string, destinationFilePath?: string): Promise<File> |
Promise<File> |
Downloads the content from the specified URL and attempts to save it as file. |
getImage(url: string): Promise<ImageSource> |
Promise<ImageSource> |
Downloads the content from the specified URL and attempts to decode it as an image. |
getJSON<T>(url: string): Promise<T> |
Promise<T> |
Downloads the content from the specified URL as a string and returns its JSON.parse representation. |
getString(url: string): Promise<string> |
Promise<string> |
Downloads the content from the specified URL as a string. |
request(options: HttpRequestOptions): Promise<HttpResponse> |
Promise<HttpResponse> |
Makes a generic http request using the provided options and returns a HttpResponse Object. |
API References
Name | Type |
---|---|
tns-core-modules/http | Module |