{"record":{"id":"0b15789e99ee7abf","repo":"anomalyco/sst","slug":"failed-to-initialize-assets-upload-http-d-s-0b1578","errorCode":null,"errorMessage":"failed to initialize assets upload: HTTP %d %s","messagePattern":"failed to initialize assets upload: HTTP (.+?) (.+?)","errorType":"http","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/server/resource/cloudflare-worker-script.go","lineNumber":223,"sourceCode":"\n\treq, err := http.NewRequest(\"PUT\", url, &body)\n\tif err != nil {\n\t\treturn err\n\t}\n\treq.Header.Set(\"Content-Type\", \"multipart/form-data; boundary=\"+writer.Boundary())\n\treq.Header.Set(\"Authorization\", \"Bearer \"+input.ApiToken)\n\n\tclient := &http.Client{Timeout: 30 * time.Second}\n\tresp, err := client.Do(req)\n\tif err != nil {\n\t\treturn err\n\t}\n\tdefer resp.Body.Close()\n\n\t// print out response body as a string\n\tif resp.StatusCode != http.StatusOK {\n\t\tresponseBody, _ := io.ReadAll(resp.Body)\n\t\treturn fmt.Errorf(\"failed to initialize assets upload: HTTP %d %s\", resp.StatusCode, string(responseBody))\n\t}\n\treturn nil\n}\n\nfunc buildMetadata(input *WorkerScriptInputs) map[string]interface{} {\n\tmetadata := make(map[string]interface{})\n\n\t// Assets\n\tif input.Assets.Jwt != \"\" || input.Assets.Config.Headers != \"\" || input.Assets.Config.Redirects != \"\" || input.Assets.Config.HtmlHandling != \"\" || input.Assets.Config.NotFoundHandling != \"\" || input.Assets.Config.RunWorkerFirst {\n\t\tassets := make(map[string]interface{})\n\t\tif input.Assets.Jwt != \"\" {\n\t\t\tassets[\"jwt\"] = input.Assets.Jwt\n\t\t}\n\t\tconfig := make(map[string]interface{})\n\t\tif input.Assets.Config.Headers != \"\" {\n\t\t\tconfig[\"_headers\"] = input.Assets.Config.Headers\n\t\t}\n\t\tif input.Assets.Config.Redirects != \"\" {","sourceCodeStart":205,"sourceCodeEnd":241,"githubUrl":"https://github.com/anomalyco/sst/blob/a0bd20f762883e72a35caccb4896c42ce5b3f707/pkg/server/resource/cloudflare-worker-script.go#L205-L241","documentation":"Despite the message text, this is the general failure path for the Cloudflare PUT /accounts/{id}/workers/scripts/{name} API call in handleUpdate: any non-200 response is surfaced with the status code and response body. It covers auth failures, bad metadata/bindings, invalid script content, oversized uploads, and account/script name problems. Raised during Create and Update of the WorkerScript resource.","triggerScenarios":"client.Do succeeds but resp.StatusCode != 200 on PUT https://api.cloudflare.com/client/v4/accounts/{AccountId}/workers/scripts/{ScriptName}; common statuses: 403 (invalid/insufficient apiToken), 400 (bad metadata, invalid bindings, missing mainModule), 404 (wrong accountId), 413 (script too large).","commonSituations":"Expired or wrongly-scoped Cloudflare API token; wrong accountId or scriptName; ES module worker uploaded without mainModule set; compatibility date/flags rejected; script exceeding size limits; transient Cloudflare 5xx.","solutions":["Read the response body in the error — Cloudflare includes an errors[] array with codes explaining the rejection","Verify apiToken is valid and has Workers Scripts:Edit permission for the account","Confirm accountId and scriptName are correct and the script exists if updating","If using ES modules, ensure mainModule is set in inputs","Retry on 5xx; fix inputs on 4xx"],"exampleFix":"// before: mainModule empty for an ES module worker\n{\"content\": {...}}\n// after: set mainModule so content-type becomes application/javascript+module\n{\"content\": {...}, \"mainModule\": \"index.js\", \"compatibilityDate\": \"2024-01-01\"}","handlingStrategy":"retry","validationCode":"if input.AccountId == \"\" || input.ScriptName == \"\" { return errors.New(\"accountId and scriptName are required\") }\nif input.ApiToken == \"\" { return errors.New(\"apiToken is required\") }\nif input.MainModule == \"\" { log.Println(\"warning: uploading as classic script (application/javascript)\") }","typeGuard":null,"tryCatchPattern":"var netErr net.Error\nif errors.As(err, &netErr) || strings.Contains(err.Error(), \"HTTP 5\") || strings.Contains(err.Error(), \"HTTP 429\") {\n    // retry with backoff\n} else if strings.Contains(err.Error(), \"HTTP 4\") {\n    // do not retry; fix token/inputs using the response body\n}","preventionTips":["Pre-validate the API token with GET /user/tokens/verify before deploying","Confirm accountId via the dashboard or GET /accounts","Set mainModule for ES module workers and a valid compatibilityDate","Keep script size under Cloudflare limits (1MB free / 10MB paid gzip-free)"],"tags":["cloudflare","http","workers","api-error","deploy"],"backgroundTag":"http-non-2xx-response","analyzedSha":"a0bd20f762883e72a35caccb4896c42ce5b3f707","analyzedAt":"2026-08-30T11:26:00.383Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}