googleapis/mcp-toolbox · error

failed to create request: %w

Error message

failed to create request: %w

What it means

getStream builds the http POST request to the Conversational Analytics streaming endpoint with http.NewRequest. If request construction fails (typically a malformed URL string, e.g. unparsable characters or an invalid base URL), this wrapped error is returned before any network activity.

Source

Thrown at internal/tools/looker/lookerconversationalanalytics/lookerconversationalanalytics.go:433

type AnalysisMessage struct {
	Query         AnalysisQuery `json:"query,omitempty"`
	ProgressEvent AnalysisEvent `json:"progressEvent,omitempty"`
}

// ErrorResponse represents an error message from the API.
type ErrorMessage struct {
	Text string `json:"text"`
}

func getStream(ctx context.Context, client *http.Client, url string, payload CAPayload, headers map[string]string) ([]map[string]any, error) {
	payloadBytes, err := json.Marshal(payload)
	if err != nil {
		return nil, fmt.Errorf("failed to marshal payload: %w", err)
	}

	req, err := http.NewRequest("POST", url, bytes.NewBuffer(payloadBytes))
	if err != nil {
		return nil, fmt.Errorf("failed to create request: %w", err)
	}
	for k, v := range headers {
		req.Header.Set(k, v)
	}

	resp, err := client.Do(req)
	if err != nil {
		return nil, fmt.Errorf("failed to send request: %w", err)
	}
	defer resp.Body.Close()

	if resp.StatusCode != http.StatusOK {
		body, _ := io.ReadAll(resp.Body)
		return nil, fmt.Errorf("API returned non-200 status: %d %s", resp.StatusCode, string(body))
	}

	var messages []map[string]any
	decoder := json.NewDecoder(resp.Body)

View on GitHub (pinned to 8cc6e09de2)

Solutions

  1. Verify the API endpoint URL is a valid absolute https URL and log it before the call
  2. Trim whitespace/control characters from the configured base URL
  3. Fix the configuration providing the URL (env var or config field)
  4. Check the wrapped %w error for the exact parse failure from net/url

Example fix

// before
endpoint := " https://cloudaicompanion.googleapis.com "
// after
endpoint := strings.TrimSpace("https://cloudaicompanion.googleapis.com")
Defensive patterns

Strategy: validation

Validate before calling

if u, err := url.Parse(endpoint); err != nil || u.Scheme == "" || u.Host == "" {
	return fmt.Errorf("invalid endpoint URL: %q", endpoint)
}

Try / catch

_, err := http.NewRequest("POST", endpoint, body)
if err != nil {
	return fmt.Errorf("check endpoint config: %w", err)
}

Prevention

When it happens

Trigger: Calling getStream with a url string that fails url.Parse — e.g. a misconfigured Conversational Analytics API base URL containing spaces or control characters, or an empty/invalid endpoint assembled from config.

Common situations: Wrong or malformed GOOGLE_CLOUD_API_ENDPOINT-style base URL in configuration; template interpolation injecting whitespace or invalid characters; proxy/region overrides producing bad URLs.

Understand the failure class

Background: "Invalid URL" / "URL cannot be empty": fix the malformed or missing URL behind request-construction failures — this error's family across 50 libraries.

Related errors


AI-assisted analysis of googleapis/mcp-toolbox@8cc6e09de2 (2026-09-05). Data as JSON: /api/errors/89f9310a8b13ffb8. Report an issue: GitHub.