Http provides utility methods to make all sorts of http networking requests.
Using Http GET request with a response body as a string A simple way to make a GET request returning a response as a string is to use the getString() method:
tsHttp.getString('https://httpbin.org/get').then(
(result: string) => {
// do something with the string response
},
(e) => {},
)
GET request with a response body as JSON
Use the getJSON() method for a GET request with a response as a JSON:
tsHttp.getJSON('https://httpbin.org/get').then(
(result) => {
console.log(result)
},
(e) => {},
)
GET request with a response body as a file
Use the getFile() method for a GET request with a response as a File:
tsHttp.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) => {},
)
GET request with a response body as an image
Use the getImage() method for a GET request with a response as an image(ImageSource):
tsHttp.getImage('https://httpbin.org/image/jpeg').then(
(res: ImageSource) => {},
(e) => {},
)
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:
tsHttp.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
},
)
API getString() ts
Http.getString(url: string): Promise<string>
Downloads the content from the specified URL as a string.
getJSON() tsHttp.getJSON<T>(url: string): Promise<T>
Downloads the content from the specified URL as a string and returns its JSON.parse representation.
getImage() tsHttp.getImage(url: string): Promise<ImageSource>
Downloads the content from the specified URL and attempts to decode it as an image.
getFile() tsHttp.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.
HttpRequestOptions interface The HttpRequestOptions interface has the following members:
url tsconst requestOptions = {
url: 'https://httpbin.org',
}
A string value representing the request url.
method tsconst requestOptions = {
url: 'https://httpbin.org',
method: 'POST',
}
Gets or sets the request method.
tsconst requestOptions = {
headers: { 'header-name': 'header-value' },
}
Optional: gets or sets the request headers in JSON format.
content tsconst requestOptions = {
content: formData,
}
//or
const requestOptions = {
content: 'some string',
}
Optional: gets or sets the request body.
timeout tsconst requestOptions = {
timeout: 22333,
}
Optional: gets or sets the request timeout in milliseconds.
dontFollowRedirects tsconst requestOptions = {
dontFollowRedirects: true,
}
Optional: boolean value that sets wether to not follow server's redirected request.
HttpResponse interface statusCode Gets the response status code.
Type: number
Gets the response content. Type: HttpContent
Gets the response headers
HttpContent interface raw Gets the response body as raw data.
toString() tsresponse.content?.toString()
Gets the response body as string.
toJSON() tsresponse.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.
toFile() tsconst 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.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4