Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse javascript Blob in golang

Tags:

javascript

go

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.

like image 747
Federico Avatar asked Aug 07 '26 16:08

Federico


1 Answers

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.

like image 141
Mark Avatar answered Aug 10 '26 06:08

Mark



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!