Skip to content

Upload a file

HTTP upload file usually use the multipart/form-data content type or send the file as a binary data.

  • Send a file as a multipart form data
  • Send a file as a binary data

Send a file as multipart form data

nv,no_run

After run navi run main.nv will output:

json

In this case, we open the file using the fs.open function and pass the file object to the Multipart.append method to append the file to the multipart form data. Then we set the multipart form data to the request using the Request.set_multipart method.

Send a file as a binary data

Sometimes, the HTTP server may only accept the file as a binary data, you can use the File type to read the file and send it as a binary data.

nv,no_run

After navi run above code, the output will be:

json

In this case, we open the file using the fs.open function and pass the file object to the Request.set_body method to set the file as the request body.