{"record":{"id":"b6eff4c9974e0fd8","repo":"googleapis/mcp-toolbox","slug":"failed-to-create-http-request-w","errorCode":null,"errorMessage":"failed to create http request: %w","messagePattern":"failed to create http request: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/sources/databaseinsights/databaseinsights.go","lineNumber":373,"sourceCode":"}\n\n// BatchQueryIndexRecommendationsResponse contains index recommendations.\ntype BatchQueryIndexRecommendationsResponse struct {\n\tDatabaseIndexRecommendations []DatabaseIndexRecommendation `json:\"databaseIndexRecommendations\"`\n}\n\n// FetchQueryStats executes the FetchQueryStats REST API method.\nfunc (s *Source) FetchQueryStats(ctx context.Context, req *FetchQueryStatsRequest) (*FetchQueryStatsResponse, error) {\n\turl := fmt.Sprintf(\"%s/v1beta/%s/queryStats:fetch\", s.getEndpointForParent(req.Parent), req.Parent)\n\n\tbodyBytes, err := json.Marshal(req)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to marshal request: %w\", err)\n\t}\n\n\thttpReq, err := http.NewRequestWithContext(ctx, \"POST\", url, bytes.NewBuffer(bodyBytes))\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to create http request: %w\", err)\n\t}\n\thttpReq.Header.Set(\"Content-Type\", \"application/json\")\n\n\tresp, err := s.httpClient.Do(httpReq)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to execute request: %w\", err)\n\t}\n\tdefer resp.Body.Close()\n\n\tif resp.StatusCode != http.StatusOK {\n\t\trespBody, _ := io.ReadAll(resp.Body)\n\t\treturn nil, fmt.Errorf(\"request failed with status %s: %s\", resp.Status, string(respBody))\n\t}\n\n\tvar fetchResp FetchQueryStatsResponse\n\tif err := json.NewDecoder(resp.Body).Decode(&fetchResp); err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to decode response: %w\", err)\n\t}","sourceCodeStart":355,"sourceCodeEnd":391,"githubUrl":"https://github.com/googleapis/mcp-toolbox/blob/8cc6e09de2ad7b8bffc77751799585a1401a48eb/internal/sources/databaseinsights/databaseinsights.go#L355-L391","documentation":"FetchQueryStats builds an outbound POST request with http.NewRequestWithContext. If request construction fails (malformed URL, invalid context), this wrapped error is returned before any network call is made.","triggerScenarios":"http.NewRequestWithContext fails, almost always because the composed URL '%s/v1beta/%s/queryStats:fetch' is malformed — e.g. an endpoint string containing spaces/invalid characters or an improperly escaped parent resource name.","commonSituations":"A misconfigured custom endpoint (getEndpointForParent returning a bad base URL); a req.Parent containing characters that break URL parsing; a canceled/invalid context in exotic setups.","solutions":["Log/inspect the wrapped error and the full URL being built","Validate s.getEndpointForParent output is a well-formed https:// URL without trailing spaces","Validate req.Parent matches the expected 'projects/*/locations/*/...' resource format","Ensure the parent is URL-escaped if it contains user-provided segments"],"exampleFix":"// before\nreq.Parent = \"projects/my project/locations/us\" // space breaks URL parsing\n// after\nreq.Parent = \"projects/my-project/locations/us\"","handlingStrategy":"validation","validationCode":"func validParent(p string) bool {\n  re := regexp.MustCompile(`^projects/[A-Za-z0-9-]+/locations/[a-z0-9-]+$`)\n  return re.MatchString(p)\n}\nif !validParent(req.Parent) { return errors.New(\"invalid parent resource name\") }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Validate parent resource names with a regex before calling","Keep endpoint configuration free of whitespace/trailing slashes","Escape user-supplied path segments with url.PathEscape"],"tags":["http","url","request-construction"],"backgroundTag":"malformed-url","analyzedSha":"8cc6e09de2ad7b8bffc77751799585a1401a48eb","analyzedAt":"2026-09-05T01:10:36.887Z","contentChangedAt":"2026-09-05T01:10:36.887Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}