From d9fe61abf4bb14c43d834605ffba62aa4a53ce12 Mon Sep 17 00:00:00 2001 From: Yosef Lin Date: Fri, 22 Sep 2023 09:34:37 +0800 Subject: [PATCH 1/2] add other method support on opts --- client.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/client.go b/client.go index 61772b6..f0db496 100644 --- a/client.go +++ b/client.go @@ -55,6 +55,8 @@ type Client struct { mu sync.Mutex EncodingBase64 bool Connected bool + Method string + Body io.Reader } // NewClient creates a new client @@ -65,6 +67,8 @@ func NewClient(url string, opts ...func(c *Client)) *Client { Headers: make(map[string]string), subscribed: make(map[chan *Event]chan struct{}), maxBufferSize: 1 << 16, + Method: http.MethodGet, + Body: nil, } for _, opt := range opts { @@ -289,7 +293,7 @@ func (c *Client) OnConnect(fn ConnCallback) { } func (c *Client) request(ctx context.Context, stream string) (*http.Response, error) { - req, err := http.NewRequest("GET", c.URL, nil) + req, err := http.NewRequest(c.Method, c.URL, c.Body) if err != nil { return nil, err } From 8d842b26635786e64f8bb2180fd6549e8717d8b7 Mon Sep 17 00:00:00 2001 From: Yosef Lin Date: Sun, 24 Sep 2023 13:08:27 +0800 Subject: [PATCH 2/2] update readme for post method --- README.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/README.md b/README.md index c2201be..5d0e5c3 100644 --- a/README.md +++ b/README.md @@ -167,6 +167,33 @@ func main() { } ``` +#### Use POST method + +To set custom query parameters on the client or disable the stream parameter altogether: + +```go +func main() { + + params := map[string]string{ + "key": "value", + } + data := url.Values{} + for k, v := range params { + data.Add(k, v) + } + client := sse.NewClient("http://server/events?search=example", func(c *sse.Client) { + c.Headers["Content-Type"] = "application/x-www-form-urlencoded" + c.Method = http.MethodPost + c.Body = strings.NewReader(data.Encode()) + }) + + client.SubscribeRaw(func(msg *sse.Event) { + // Got some data! + fmt.Println(msg.Data) + }) +} +``` + ## Contributing