Convert text to file and send to server

I want to make an API that can receive large amounts of data and am using packages like
Is it possible to convert form data into a file and then send it to the server?

You just include the data you want to send in a request body, you can encode the data any way you want, the easiest way to encode the data would probably be using JSON, YAML or any other text encoding/decoding definitions.

However, if you’re a power user you might want to look into sending binary data using the UInt8Array object.

This is essentially the same as file uploads, although when a file-upload is performed the browser will read the file you want to upload and take all of that content into a request body for you.

1 Like

Yeah, I thought so - I did some research about the data URI. Is there a way to compress and send an Array length 10,000 into a url like /api/:data?

Perhaps not in the URL. however, you should be able to have an endpoint similar to something like /api/do-something. What is the kind of data you want to send to the server? Is it user-input, is the input fixed or dynamic?

1 Like

It would be user-input - I at first wanted to be able to send everything to an API without any client-side code, but I’m starting to think that would be very difficult.

Is the input fixed? What sort of inputs are you sending, if the input isn’t fixed, or dynamically fixed I recommend just using JSON and send the data in a post request:

await fetch("/api/do-something", {
  method: "POST",
  body: JSON.stringify(data),
  headers: {
    "Content-Type": "application/json",
    "Accepts": "application/json"

If the input is fixed I recommend converting the data to binary to save space.

1 Like

The input would be user-defined - they would be able to do something like fetch("api.mysite/api/4ja6c6f6g6b7c7273747").then(do something).
The max length of a URl is 2048 chars - could work :thinking:

What I mean by whether the input is fixed is if there’s a specific definition on the user-input.

For instance, how the object looks like, and the property rules.

If I have to make an example on this I’d say a login/register form.

I’ve recently myself started turning form data into binary data using js, I’d separate the login name and password using character \000 and just sent it in a UInt8Array.

1 Like