title |
---|
Http |
Http provides utility methods to make all sorts of http networking requests.
A simple way to make a GET request returning a response as a string is to use the getString() method:
Http.getString('https://httpbin.org/get').then(
(result: string) => {
// do something with the string response
},
(e) => {},
)
Use the getJSON() method for a GET request with a response as a JSON:
Http.getJSON('https://httpbin.org/get').then(
(result) => {
console.log(result)
},
(e) => {},
)
Use the getFile() method for a GET request with a response as a File:
Http.getFile(
'https://art.nativescript.org/logo/export/NativeScript_Logo_Wide_White_Blue_Rounded_Blue.png',
).then(
(resultFile: File) => {
// The returned result will be File object
},
(e) => {},
)
Use the getImage() method for a GET request with a response as an image(ImageSource):
Http.getImage('https://httpbin.org/image/jpeg').then(
(res: ImageSource) => {},
(e) => {},
)
:::tip Note
The above methods can also take, instead of the url string, an HttpRequestOptions object. Also, alternatively to the above methods, you can use the request method and call the appropriate(e.g .toJSON()
, .toString()
) method on the HttpResponse object's content
to get the response type you want.
:::
To make a POST request, use the request() method:
Http.request({
url: 'https://httpbin.org/get',
method: 'POST',
headers: { 'Content-Type': 'application/json' },
content: JSON.stringify({
username: 'tony',
password: 'pass',
}),
}).then(
(response) => {
const result = response.content?.toJSON()
},
(e) => {
// error
},
)
Http.getString(url: string): Promise<string>
Downloads the content from the specified URL as a string.
Http.getJSON<T>(url: string): Promise<T>
Downloads the content from the specified URL as a string and returns its JSON.parse representation.
Http.getImage(url: string): Promise<ImageSource>
Downloads the content from the specified URL and attempts to decode it as an image.
Http.getFile(url: string, destinationFilePath?: string): Promise<File>
Downloads the content from the specified URL and attempts to save it as file. destinationFilePath
is an optional location of where you want to save the returned file.
Http.request(options: HttpRequestOptions): Promise<HttpResponse>
Makes a generic http request using the provided options and returns a HttpResponse Object.
The HttpRequestOptions interface has the following members:
const requestOptions = {
url: 'https://httpbin.org',
}
A string value representing the request url.
const requestOptions = {
url: 'https://httpbin.org',
method: 'POST',
}
Gets or sets the request method.
const requestOptions = {
headers: { 'header-name': 'header-value' },
}
Optional: gets or sets the request headers in JSON format.
const requestOptions = {
content: formData,
}
//or
const requestOptions = {
content: 'some string',
}
Optional: gets or sets the request body.
const requestOptions = {
timeout: 22333,
}
Optional: gets or sets the request timeout in milliseconds.
const requestOptions = {
dontFollowRedirects: true,
}
Optional: boolean value that sets wether to not follow server's redirected request.
Gets the response status code.
Type: number
Gets the response content. Type: HttpContent
Gets the response headers
response.content?.raw
Gets the response body as raw data.
response.content?.toString()
Gets the response body as string.
response.content?.toJSON(encoding)
Gets the response body as JSON object. The encoding
is optional of type 'UTF8' | 'GBK'
.
response.content
?.toImage()
.then((image: ImageSource) => {})
.catch((error) => {})
Gets the response body as ImageSource.
const file: File = response.content?.toFile(destinationFilePath)
Gets the response body as a file. destinationFilePath
is an optional location of where you want to save the returned file.