英文:
Parse javascript Blob in golang
问题
在Go语言中,你可以使用r.ParseMultipartForm()
方法来读取通过Ajax和FormData发送的表单数据,该方法会将表单请求数据填充到Form
映射中。
func form(w http.ResponseWriter, r *http.Request) {
r.ParseMultipartForm(500)
fmt.Fprintf(w, "This is the value of %+v", r.Form)
}
然而,我还没有找到一种解析Blob的方法。上面的代码在我发送Blob而不是表单时返回一个空映射。也就是说,当我发送以下内容时:
var blob = new Blob([JSON.stringify(someJavascriptObj)]);
//XHR initialization, etc. etc.
xhr.send(blob);
上述Go代码不起作用。但是,当我发送以下内容时:
var form = new FormData(document.querySelector("form"));
//...
xhr.send(form);
我可以正常读取表单数据。
英文:
In Go, you can read a form sent using Ajax and FormData using r.ParseMultipartForm()
, which populates the Form
map with form request data.
func form(w http.ResponseWriter, r *http.Request) {
r.ParseMultipartForm(500) //
fmt.Fprintf(w, "This is the value of %+v", r.Form)
}
However, I haven't found a method to parse Blobs. The above code returns an empty map whenever instead of sending a form, I send a Blob. That is, when I send this:
var blob = new Blob([JSON.stringify(someJavascriptObj)]);
//XHR initialization, etc. etc.
xhr.send(blob);
the Go code above doesn't work. Now, when I send this:
var form = new FormData(document.querySelector("form"));
//...
xhr.send(form);
I can read form data without problems.
答案1
得分: 1
r.ParseMultipartForm(500)
这里可能返回了一个错误?尝试捕获错误:
if err := r.ParseMultipartForm(500); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
此外,考虑将500字节的内存限制提高,因为较大的数据块将被写入临时文件。
英文:
r.ParseMultipartForm(500)
Perhaps an error is being returned here? Try capturing the error:
if err := r.ParseMultipartForm(500); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
Also, consider raising the 500 byte memory limit as larger blobs will be written to temporary files.
答案2
得分: 1
我认为JavaScript将blob视为文件,所以你可以在r.MultipartForm.File
中查找它,获取文件头,打开它,读取、解码和解析它。
例如,尝试以下代码:
r.ParseMultipartForm(500)
fmt.Fprintf(w, "This is the value of %+v", *r.MultipartForm.File)
}
请注意,这是一个示例代码,具体实现可能会有所不同。
英文:
I think javascript treats blob as file, so your can look it in r.MultipartForm.File
, get file header, open it, read, decode and parse.
Try for example
r.ParseMultipartForm(500)
fmt.Fprintf(w, "This is the value of %+v", *r.MultipartForm.File)
}
答案3
得分: 0
我假设JavaScript的Blob是一个十六进制字符串,最终可以转换为Go中JSON的标准类型[]byte
。
// 一旦你获取到Blob
blobString := 7b22666f6f223a205b22626172222c202262617a222c2039395d7d
b, _ := hex.DecodeString(blobString)
json := string(b)
fmt.Println(json) // 输出 {"foo": ["bar", "baz", 99]}
如果你的Blob来自JavaScript,你可能需要查看encoding/hex
和encoding/binary
包,将其解码为Go中的[]byte
类型。
英文:
I presume Javascript's Blob is a hex string which can eventually be converted to []byte
, which is a standard type for JSON in Go.
// Once you get the blob
blobString := `7b22666f6f223a205b22626172222c202262617a222c2039395d7d`
b, _ := hex.DecodeString(blobString)
json := string(b)
fmt.Println(json) // prints out {"foo": ["bar", "baz", 99]}
You might want to look into encoding/hex
and encoding/binary
packages to decode your blob acquired from Javascript to type []byte
in Go, if it's not already.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论