| 196 | } |
| 197 | |
| 198 | func (w *WebhookConfig) send(ctx context.Context, body io.Reader) ([]byte, error) { |
| 199 | req, err := http.NewRequestWithContext(ctx, http.MethodPost, w.URL, body) |
| 200 | if err != nil { |
| 201 | return nil, fmt.Errorf("creating request: %w", err) |
| 202 | } |
| 203 | |
| 204 | req.Header.Set("Content-Type", w.CType) |
| 205 | |
| 206 | res, err := w.client.Do(req) |
| 207 | if err != nil { |
| 208 | return nil, fmt.Errorf("POSTing payload: %w", err) |
| 209 | } |
| 210 | defer res.Body.Close() |
| 211 | |
| 212 | // The error is mostly ignored because we don't care about the body. |
| 213 | // Read it in to avoid a memopry leak. Used in the if-stanza below. |
| 214 | reply, _ := io.ReadAll(res.Body) |
| 215 | |
| 216 | if res.StatusCode < http.StatusOK || res.StatusCode > http.StatusNoContent { |
| 217 | return nil, fmt.Errorf("%w (%s): %s", ErrInvalidStatus, res.Status, reply) |
| 218 | } |
| 219 | |
| 220 | return reply, nil |
| 221 | } |
| 222 | |
| 223 | func (u *Unpackerr) validateWebhook() error { //nolint:cyclop |
| 224 | for idx := range u.Webhook { |