If the file is small (some KB) and is always a plain text file (.txt) you can convert the content to Base64 and send to payload of the JSON object.
The downside of this method is the effort to convert the file to Base64 on the client and then convert to text again on the server. In addition, the resulting Base64 string would be about 1/3 larger than the original file. For this reason, I would only use this method for files of a few KB and over which I had full control of the request in the client.
If you do not have control of what is being sent, a malicious client could send a request with a multi-MB file encoded in Base64 and cause problems to your server.
If you want a more elaborate solution that meets any type and file size, I suggest you make the client submit two requests. In the first, you send only the attributes. The entity is created and returns the URL for uploading the image.
Example of the first request:
PUT /restapi/v1/clientes HTTP/1.1
Host: www.seuhost.com.br/
{
"nomeCliente" : "Jean",
//Outros atributos
}
Return with status 201 (created):
{
meta: {
},
data: {
"urlArquivo": "http://www.seuhost2.com.br/apirest/v1/clientes/id/123456/arquivo"
}
}
The client would then use this URL to send the file.
This way, you can create specific controls on the uploaded file. You can even determine that all files are sent to another server, so as not to jam the original application server.
It is not possible to send a file through JSON, except if you convert it to Base64, for example.
– Valdeir Psr
even if I change the enctype attribute of the form tag to: enctype="Multipart/form-data"
– JeanKBN