Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Support for easier parsing and creation of Cookie #1174

Closed
tamalsaha opened this issue Apr 6, 2017 · 2 comments
Closed

Support for easier parsing and creation of Cookie #1174

tamalsaha opened this issue Apr 6, 2017 · 2 comments

Comments

@tamalsaha
Copy link

We can get the header keys from Metadata. But the related parsing code from Cookies in private in net/http package. Is there any way those can be exposed from Metadata or net/http package?
https://golang.org/src/net/http/cookie.go#L198

Thanks.

@menghanl
Copy link
Contributor

menghanl commented Apr 6, 2017

We don't have support for that.
But I believe you should be able to do something like

cookies := (&http.Request{Header: metadata}).Cookies()

@menghanl menghanl closed this as completed Apr 6, 2017
@tamalsaha
Copy link
Author

tamalsaha commented Apr 10, 2017

I tried the above code snippet. It does not work. metadata.MD converts headers keys into lower case. On the other hand Request.Header, converts all keys to upper case.So, you need to do the following to make things work:

package auth

import (
	"fmt"
	"net/http"
	"testing"

	"google.golang.org/grpc/metadata"
)

func TestGetFromCookie(t *testing.T) {
	md := metadata.Pairs("Key-A", "Val-A")
	req := &http.Request{
		Header: http.Header{},
	}
	for k, v := range md {
		for _, val := range v {
			req.Header.Add(k, val)
		}
	}
	// req := &http.Request{Header: http.Header(md)}
	fmt.Println(req.Header.Get("Key-A"))
}

@lock lock bot locked as resolved and limited conversation to collaborators Sep 26, 2018
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants