Skip to content

Download a file

You can download a file from the server using the HttpClient.get method.

nv,no_run

After run navi run main.nv, we will download and save image.png file.

In this case:

  • We use the [fs.create] to open a file with WRITE mode, if the file is not exists, it will create a new file.
  • The defer statement is used to close the file after the function returns (Like defer in Go).
  • We create an HTTP client using the HttpClient.new function.
  • The HttpClient.get method is used to send a GET request to the server and get the response.
  • Copy the response body to the file using the io.copy function.