将复杂的负载传递给服务器,包括”text/csv”和”application/json”。

huangapple go评论75阅读模式
英文:

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

问题

{
"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?

英文:

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

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

What would be the most appropriate way to do it?
If I try to do with &quot;Content-Type&quot;: &quot;application/json&quot;, 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?

答案1

得分: 1

JSON数据格式不支持ArrayBuffer数据类型。它支持对象、数组、字符串、数字、true、false和null。

如果您想将CSV嵌入到JSON中,那么它需要是一个字符串。CSV文件是文本文件,所以可以轻松地放入字符串中。

(如果您想在JSON中包含二进制数据,那么您需要将其编码为字符串,例如使用base64,但在CSV中不需要这样做)。

英文:

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).

huangapple
  • 本文由 发表于 2023年1月4日 01:15:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/74996546.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定