mirror of
https://github.com/nlepage/go-wasm-http-server.git
synced 2025-03-10 17:29:10 +00:00
🚧
This commit is contained in:
parent
e0ce5c6341
commit
3919f17a99
30
request.go
Normal file
30
request.go
Normal 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
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user