Websocket : Bad Handshake | Response 400 Bad Request

saurabh2 years ago

Got JSESSIONID after calling /api/session, then calling /api/socket but getting 400 Bad Request error. What's going wrong?

Cookie format that's getting passed in header : JSESSIONID=node0losdgdd5ed0je74vbnfqynav4559.node0; Path=/

Any help would be greatly appreciated! Here's the code chunk (golang)

    var addr = flag.String("addr", "demo3.traccar.org:443", "http service address")
    apiUrl := "https://" + *addr
	resource := "/api/session/"
	data := url.Values{}
	data.Set("email", "--email--")
	data.Set("password", "--pass--")

	u, _ := url.ParseRequestURI(apiUrl)
	u.Path = resource
	urlStr := u.String()

	client := &http.Client{}
	r, _ := http.NewRequest(http.MethodPost, urlStr, strings.NewReader(data.Encode()))
	resp, _ := client.Do(r)

	header := http.Header{}
	header.Add("Cookie", resp.Cookies()[0].String())
	fmt.Println(resp.Cookies()[0])

	interrupt := make(chan os.Signal, 1)
	signal.Notify(interrupt, os.Interrupt)

	uNew := url.URL{Scheme: "ws", Host: *addr, Path: "/api/socket"}
	log.Printf("connecting to %s", uNew.String())

	d := websocket.Dialer{}
	c, resp, err := d.Dial(uNew.String(), header)
	if err != nil {
		log.Fatal("dial:", err, resp)
	}
	defer c.Close()
saurabh2 years ago

Without the :443 port, getting 503 Service Unavailable

Anton Tananaev2 years ago

What's the response payload?

saurabh2 years ago

Thanks Anton, got the fix : Added the cookies to websocket dialer and getting the correct response now
dialer.Jar.SetCookies(&uNew, []*http.Cookie{&cookie})