从第二个goroutine中编写HTTP响应。

huangapple go评论83阅读模式
英文:

Writing http response from an second goroutine

问题

我一直在使用 Spotify API 进行尝试,并遇到了一个问题。代码中使用了 context.Context,因此函数会“随机”执行。OAuth 函数应该检查代码是否无效,但如果我不使用通道,那么代码的最后部分会直接执行,而不会等待第一个/第二个函数完成。因此,我创建了一个第二个 goroutine,检查通道是否接收到并写入响应。但现在我遇到了这个错误 http: wrote more than the declared Content-Length,我该如何修正 Content-Length?为什么要使用 context?

我的代码:

// Wrapper: github.com/zmb3/spotify/v2

func WriteResponse(w http.ResponseWriter, h chan *spotify.Client) {

	client := <-h

	user, err := client.CurrentUser(context.Background())
	fmt.Println(user.User.DisplayName)
	if err != nil {
		_, err := fmt.Fprint(w, "Couldn't get user sorry :(")
		if err != nil {
			return
		}
	}
	_, err = fmt.Fprintf(w, "Logged in as %s!", user.User.DisplayName)
	if err != nil {
		log.Println(err)
		return
	}
}

func OAuth(w http.ResponseWriter, r *http.Request) {
	ch := make(chan *spotify.Client)

	tok, err := auth.Token(r.Context(), state, r)
	if err != nil {
		w.WriteHeader(503)
		_, err := fmt.Fprint(w, "Couldn't get token sorry :(")
		if err != nil {
			return
		}
	}

	if st := r.FormValue("state"); st != state {
		http.NotFound(w,r)
		log.Fatalf("State mismatch: %s != %s\n", st, state)
	}

	go WriteResponse(w, ch)
	client := spotify.New(auth.Client(r.Context(), tok))

	ch <- client
}

希望这能帮到你!如果你有任何其他问题,请随时问我。

英文:

I've been playing around with the spotify api and came to an Problem. context.Context gets used and therefore the functions just "randomly" execute. The OAuth function should check if the Code is invalid but If I don't do this with an channel the last part of the code gets executed directly without even the first/second function finishing. Because of that I made an second goroutine that checks if the channel is received and then write an response. But now I get this error http: wrote more than the declared Content-Length how can I correct the Content-Lenght? Why is context even used?

My Code:

// Wrapper: github.com/zmb3/spotify/v2

func WriteResponse(w http.ResponseWriter, h chan *spotify.Client) {

	client := &lt;-h

	user, err := client.CurrentUser(context.Background())
	fmt.Println(user.User.DisplayName)
	if err != nil {
		_, err := fmt.Fprint(w, &quot;Couldn&#39;t get user sorry :(&quot;)
		if err != nil {
			return
		}
	}
	_, err = fmt.Fprintf(w, &quot;Logged in as %s!&quot;, user.User.DisplayName)
	if err != nil {
		log.Println(err)
		return
	}
}

func OAuth(w http.ResponseWriter, r *http.Request) {
	ch := make(chan *spotify.Client)

	tok, err := auth.Token(r.Context(), state, r)
	if err != nil {
		w.WriteHeader(503)
		_, err := fmt.Fprint(w, &quot;Couldn&#39;t get token sorry :(&quot;)
		if err != nil {
			return
		}
	}

	if st := r.FormValue(&quot;state&quot;); st != state {
		http.NotFound(w,r)
		log.Fatalf(&quot;State mismatch: %s != %s\n&quot;, st, state)
	}

	go WriteResponse(w, ch)
	client := spotify.New(auth.Client(r.Context(), tok))

	ch &lt;- client
}

答案1

得分: 0

你忘记返回了。。

if err != nil {
        w.WriteHeader(503)
        _, err := fmt.Fprint(w, "无法获取令牌,抱歉 :(")
        if err != nil {
            return
        }
        // 这里
        return
    }
英文:

You forgot to return..

if err != nil {
        w.WriteHeader(503)
        _, err := fmt.Fprint(w, &quot;Couldn&#39;t get token sorry :(&quot;)
        if err != nil {
            return
        }
        // here
        return
    }

huangapple
  • 本文由 发表于 2022年1月23日 17:39:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/70820804.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定