我正在尝试使用 Go 的net/http包从 Go/wasm 发送请求(我不确定是否应该使用来自 wasm 的 javascript 的 fetch 函数)。即使我可以正确看到浏览器上的所有标头和 cookie(在浏览器的网络选项卡上,我也可以看到所有带有 curl 请求的标头),但我无法从 Go/WASM 访问所有响应标头和 Cookie。当我尝试打印所有标题时,我只能在控制台上看到 2 个标题。它们是 "Content-Length" 和 "Content-Type" 。有谁知道这是什么原因?
这是服务器端的示例代码:
import "github.com/gorilla/sessions"
var store = sessions.NewCookieStore([]byte("super-secret-key-4"))
func (a *App) TestHandler(w http.ResponseWriter, r *http.Request) {
cookieSession, _ := store.Get(r, "session")
cookieSession.Values["test"] = "test"
cookieSession.Save(r, w)
w.Header().Set("Test", "test")
io.WriteString(w, `{"test":"test"}`)
return
}
客户端:
func TestRequest(userName string) {
type Payload struct {
Name string `json:"name"`
}
payload := Payload{
Name: userName,
}
payloadBytes, _ := json.Marshal(payload)
body := bytes.NewReader(payloadBytes)
req, _:= http.NewRequest("POST","localhost:8080/Test", body)
req.Header.Set("Content-Type", "application/json")
resp, _:= http.DefaultClient.Do(req)
//a, _ := ioutil.ReadAll(resp.Body)
//bodyString := string(a)
for name, values := range resp.Header {
for _, value := range values {
log.Println(name, value)
}
}
for _, cookie := range resp.Cookies() {
log.Println(cookie.Name)
}
defer resp.Body.Close()
}
这是我在浏览器控制台上得到的:
wasm_exec.js:51 2021/08/04 21:08:48 Content-Length 274
wasm_exec.js:51 2021/08/04 21:08:48 Content-Type text/plain; charset=utf-8