Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 5 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ type Client struct {
mu sync.Mutex
EncodingBase64 bool
Connected bool
Method string
Body io.Reader
}

// NewClient creates a new client
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down