Skip to content

Latest commit

 

History

History
299 lines (200 loc) · 4.99 KB

http.md

File metadata and controls

299 lines (200 loc) · 4.99 KB
title
Http

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:

Http.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:

Http.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:

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) => {},
)

GET request with a response body as an image

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. :::

Making a POST request

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
  },
)

API

getString()

Http.getString(url: string): Promise<string>

Downloads the content from the specified URL as a string.


getJSON()

Http.getJSON<T>(url: string): Promise<T>

Downloads the content from the specified URL as a string and returns its JSON.parse representation.


getImage()

Http.getImage(url: string): Promise<ImageSource>

Downloads the content from the specified URL and attempts to decode it as an image.


getFile()

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.


request()

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

const requestOptions = {
  url: 'https://httpbin.org',
}

A string value representing the request url.


method

const requestOptions = {
  url: 'https://httpbin.org',
  method: 'POST',
}

Gets or sets the request method.


headers

const requestOptions = {
  headers: { 'header-name': 'header-value' },
}

Optional: gets or sets the request headers in JSON format.


content

const requestOptions = {
  content: formData,
}

//or

const requestOptions = {
  content: 'some string',
}

Optional: gets or sets the request body.


timeout

const requestOptions = {
  timeout: 22333,
}

Optional: gets or sets the request timeout in milliseconds.


dontFollowRedirects

const 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


content

Gets the response content. Type: HttpContent


headers

Gets the response headers


HttpContent interface

raw

response.content?.raw

Gets the response body as raw data.


toString()

response.content?.toString()

Gets the response body as string.


toJSON()

response.content?.toJSON(encoding)

Gets the response body as JSON object. The encoding is optional of type 'UTF8' | 'GBK'.


toImage()

response.content
  ?.toImage()
  .then((image: ImageSource) => {})
  .catch((error) => {})

Gets the response body as ImageSource.


toFile()

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.