Reputation: 83
I am trying to send request from Go/wasm with Go's net/http
package (I am not sure if I should use javascript's fetch function from wasm) . I cannot reach all of the response Headers and Cookies from Go/WASM even though I can see properly all of the headers and cookies on the browser (on network tab of browser and also i can see all of the headers with curl request) . When I try to print all of the headers i can only see 2 headers on the console.Those are "Content-Length" and "Content-Type" . Does anybody knows what is the reason of this ?
Here is example code of server side:
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
}
Client Side:
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()
}
This is what i get on the browser console:
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
Upvotes: 1
Views: 515
Reputation: 83
I finally see that i need to add a special header to response for use my custom headers on client side.If i add this line to my middleware i can see my custom headers on client side.
w.Header().Set("Access-Control-Expose-Headers", "MyHeader,Myheader2")
Upvotes: 0