Home > Blockchain >  pass to the server an complex payload of both "text/csv" and "application/json"
pass to the server an complex payload of both "text/csv" and "application/json"

Time:01-04

I am trying to pass to the server a complex payload that looks like this:

{
    "name": "",
    "description": "",
    "files": [
        {
            "file_name": "test_CSV.csv",
            "file_type": "text/csv",
            "table_name": "test_CSV",
            "file_size": 1801440,
            "binary": ArrayBuffer <- this is a CSV
        }
    ]
}

What would be the most appropriate way to do it? If I try to do with "Content-Type": "application/json", the ArrayBuffer gets lost. If I try to do it with "text/csv" I cannot recover the remaining information about the file. What is the 'best-practices' in this situation?

CodePudding user response:

The JSON data format doesn't support the ArrayBuffer data type. It only supports objects, arrays, strings, numbers, true, false and null.

If you want to embed CSV into JSON then it needs to be a string. CSV files are text files so will happily fit into strings.

(If you wanted to include binary data in JSON then you'd need to encode it as a string, e.g. using base64, but there's no need for that with CSV).

  • Related