在响应中设置Access-Control-Allow-Credentials头部。

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

set the Access-Control-Allow-Credentials Header in Response

问题

我想在go-endpoints中使用Cookie。

为了实现这个目的,需要将Access-Control-Allow-Credentials设置为Header。

然而,我不知道如何在go-endpoints中设置Allow-Credentials为Header。

allowCookieAuth是什么?如何将其设置为true?

由于go-endpoints处理程序中没有http.ResponseWriter,所以无法设置Http Header。

请帮助我!

英文:

I want to use Cookie along with the go-endpoints.

For this purpose , it is necessary to set the Access-Control-Allow-Credentials to Header.

However , do not know how to set the Allow-Credentials to Header in go-endpoints.

allowCookieAuth is , How do I to true doing ?

https://github.com/GoogleCloudPlatform/go-endpoints/search?utf8=%E2%9C%93&q=allowCookieAuth

because there is no http.ResponseWriter the go-endpoints handler , it is not possible to set the Http Header.

func (gs *GreetingService) List(c endpoints.Context, r *GreetingsListReq) (*GreetingsList, error) {
	if r.Limit <= 0 {
    	r.Limit = 10
    }

 	q := datastore.NewQuery("Greeting").Limit(r.Limit)
	greets := make([]*Greeting, 0, r.Limit)
	keys, err := q.GetAll(c, &greets)
	if err != nil {
    	return nil, err
	}

    for i, k := range keys {
	    greets[i].Key = k
    }
    return &GreetingsList{greets}, nil
}

help me!

答案1

得分: 1

如果我理解你的意思正确,那么你只需要像下面这样做:

func yourHandler(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Access-Control-Allow-Credentials", "true")
    // ...其他代码
}
英文:

If I understand you correctly then all you have to do is something like:

func yourHandler(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Access-Control-Allow-Credentials", "true")
	// ...other code
}

答案2

得分: 0

通过修补go-endpoints,并达到目标。

dst.Auth = &struct {
AllowCookie bool json:"allowCookieAuth"
}{true}

https://github.com/sinmetal/go-endpoints/commit/25c0e85b06c13511291cb8b54f2878c5ac4587ad

然而,我认为这不是一个很好的方法。
如果可能的话,我希望在不进行修补的情况下达到目标。

英文:

By patching the go-endpoints, and reached the goal .

dst.Auth = &struct {
    	AllowCookie bool `json:"allowCookieAuth"`
	}{true}

https://github.com/sinmetal/go-endpoints/commit/25c0e85b06c13511291cb8b54f2878c5ac4587ad

However , I do not think that's very good way .
If possible , I want to reach the goal without patching .

huangapple
  • 本文由 发表于 2015年2月13日 18:18:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/28497275.html
匿名

发表评论

匿名网友

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

确定