bytebase/bytebase · error

failed to construct webhook POST request to %s

Error message

failed to construct webhook POST request to %s

What it means

After marshaling, postMessage builds the POST with http.NewRequest against context.URL; this error wraps any http.NewRequest failure with the target URL. NewRequest fails mainly on an unparseable URL (missing/invalid scheme) or an invalid HTTP method.

Source

Thrown at backend/plugin/webhook/slack/slack.go:224

	if context.DirectMessage && len(context.MentionEndUsers) > 0 {
		if postDirectMessage(context) {
			return nil
		}
		slog.Warn("failed to send direct messages for slack users, fallback to normal webhook", slog.String("event", context.EventType))
	}
	return postMessage(context)
}

func postMessage(context webhook.Context) error {
	post := BuildMessage(context)
	body, err := json.Marshal(post)
	if err != nil {
		return errors.Wrapf(err, "failed to marshal webhook POST request to %s", context.URL)
	}
	req, err := http.NewRequest("POST",
		context.URL, bytes.NewBuffer(body))
	if err != nil {
		return errors.Wrapf(err, "failed to construct webhook POST request to %s", context.URL)
	}

	req.Header.Set("Content-Type", "application/json")
	client := &http.Client{
		Timeout: webhook.Timeout,
	}
	resp, err := client.Do(req)
	if err != nil {
		return errors.Wrapf(err, "failed to POST webhook to %s", context.URL)
	}

	b, err := io.ReadAll(resp.Body)
	if err != nil {
		return errors.Wrapf(err, "failed to read POST webhook response from %s", context.URL)
	}
	defer resp.Body.Close()

	if resp.StatusCode != http.StatusOK {

View on GitHub (pinned to 1870550677)

Solutions

  1. Validate the Slack webhook URL is a well-formed https URL before calling Post (url.ParseRequestURI and check scheme)
  2. Fix the workspace webhook configuration so context.URL is a complete https://hooks.slack.com/... address
  3. Trim whitespace and ensure no unresolved template placeholders remain in the configured URL

Example fix

// before
u := settings.SlackWebhookURL // possibly empty
// after
u := strings.TrimSpace(settings.SlackWebhookURL)
if _, err := url.ParseRequestURI(u); err != nil || !strings.HasPrefix(u, "https://") { return errors.New("invalid slack webhook url") }
Defensive patterns

Strategy: validation

Validate before calling

u, err := url.ParseRequestURI(strings.TrimSpace(webhookURL)); if err != nil || u.Scheme != "https" { return errors.New("invalid slack webhook url") }

Try / catch

if err := webhook.Post(ctx); err != nil { if strings.Contains(err.Error(), "failed to construct webhook POST request") { /* reconfigure URL */ } }

Prevention

When it happens

Trigger: Calling Post with a webhook context whose URL is empty, lacks an http(s) scheme, or contains characters invalid in a URL.

Common situations: Slack webhook URL not configured or left empty in workspace settings; URL copied with whitespace or bad scheme (e.g. 'slack.com/hooks/...' without https://); configuration template not substituted so URL contains literal placeholders.

Understand the failure class

Background: "Invalid URL" errors: why new URL(), URI.parse, and reqwest::Url reject your string — missing scheme, whitespace, and bad path format — this error's family across 39 libraries.

Related errors


AI-assisted analysis of bytebase/bytebase@1870550677 (2026-09-06). Data as JSON: /api/errors/760d6d2250dfcf7a. Report an issue: GitHub.