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 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 }