This commit is contained in:
Nicolas Lepage 2019-11-27 00:04:00 +01:00
parent e0ce5c6341
commit 3919f17a99
No known key found for this signature in database
GPG Key ID: B0879E35E66D8F6F

30
request.go Normal file
View File

@ -0,0 +1,30 @@
package wasmhttp
import (
"bytes"
"net/http"
"syscall/js"
)
// Request is a JS Request
type Request js.Value
// HTTPRequest builds and returns this equivalent http.Request
func (r *Request) HTTPRequest() (*http.Request, error) {
rValue := js.Value(*r)
jsBody := js.Global().Get("Uint8Array").New(Promise(rValue.Call("arrayBuffer")).Await())
body := make([]byte, jsBody.Get("length").Int())
js.CopyBytesToGo(body, jsBody)
req, err := http.NewRequest(
rValue.Get("method").String(),
rValue.Get("url").String(),
bytes.NewBuffer(body),
)
if err != nil {
return nil, err
}
return req, nil
}