{"id":"cbd1cfef9ef410a1","repo":"axios/axios","slug":"err-invalid-url-cbd1cf","errorCode":"ERR_INVALID_URL","errorMessage":"Invalid URL","messagePattern":"Invalid URL","errorType":"exception","errorClass":"AxiosError","httpStatus":null,"severity":"error","filePath":"lib/helpers/fromDataURI.js","lineNumber":35,"sourceCode":" * @param {?Function} options.Blob\n *\n * @returns {Buffer|Blob}\n */\nexport default function fromDataURI(uri, asBlob, options) {\n  const _Blob = (options && options.Blob) || platform.classes.Blob;\n  const protocol = parseProtocol(uri);\n\n  if (asBlob === undefined && _Blob) {\n    asBlob = true;\n  }\n\n  if (protocol === 'data') {\n    uri = protocol.length ? uri.slice(protocol.length + 1) : uri;\n\n    const match = DATA_URL_PATTERN.exec(uri);\n\n    if (!match) {\n      throw new AxiosError('Invalid URL', AxiosError.ERR_INVALID_URL);\n    }\n\n    const type = match[1];\n    const params = match[2];\n    const encoding = match[3] ? 'base64' : 'utf8';\n    const body = match[4];\n\n    // RFC 2397 section 3: default mediatype is text/plain;charset=US-ASCII\n    // Bare `data:,` leaves mime undefined; Blob normalises that to \"\" per spec.\n    let mime = '';\n    if (type) {\n      mime = params ? type + params : type;\n    } else if (params) {\n      mime = 'text/plain' + params;\n    }\n\n    const buffer = encoding === 'base64'\n      ? Buffer.from(body, 'base64')","sourceCodeStart":17,"sourceCodeEnd":53,"githubUrl":"https://github.com/axios/axios/blob/c3f553c740ebf3dff5e22dae24e9caaafafddd2d/lib/helpers/fromDataURI.js#L17-L53","documentation":"Thrown by the data-URI parser when a URL with the `data:` protocol does not match the RFC 2397 grammar `data:[<mediatype>][;base64],<data>`. axios supports requesting data: URLs directly (mainly in Node), and this ERR_INVALID_URL means the part after `data:` is malformed — most often a missing comma separating the metadata from the payload.","triggerScenarios":"Calling axios with a URL starting with `data:` whose body fails the DATA_URL_PATTERN regex — e.g. `axios.get('data:base64AAAA')` (no comma), a mediatype without a slash plus malformed parameters, or a truncated data URI produced by string slicing.","commonSituations":"Programmatically constructing data URIs and forgetting the comma or the `;base64` marker; passing a truncated data URI (copied from devtools or cut by a logging/length limit); template bugs that concatenate `data:` with raw base64 directly.","solutions":["Fix the data URI format: it must be `data:[mime][;base64],<payload>` — e.g. `data:image/png;base64,iVBORw0...`; the comma is mandatory.","If the URI is generated, verify the full string isn't truncated (check its length against the expected payload).","If you already hold the raw bytes, skip the data URI round-trip and use the Buffer/Blob directly instead of routing it through axios."],"exampleFix":"// before — missing comma\nawait axios.get('data:image/png;base64' + b64);\n// after\nawait axios.get('data:image/png;base64,' + b64);","handlingStrategy":"validation","validationCode":"const DATA_URI = /^data:([^;,]*)?(;[^,]*)?,/i;\nif (!DATA_URI.test(uri)) {\n  throw new Error('not a well-formed data: URI');\n}","typeGuard":"function isDataURI(value) {\n  return typeof value === 'string' && /^data:[^,]*,/i.test(value);\n}","tryCatchPattern":"try {\n  const res = await axios.get(dataUri);\n} catch (err) {\n  if (axios.isAxiosError(err) && err.code === 'ERR_INVALID_URL') {\n    // the data: URI was malformed — reject the input at its source\n  } else throw err;\n}","preventionTips":["Validate data: URIs at the trust boundary (user input, config) before handing them to axios","Construct data URIs programmatically (`data:${mime};base64,${buf.toString('base64')}`) instead of string-concatenating user fragments","Never fall back to a default URI on failure — report the bad input"],"tags":["data-uri","url-parsing","validation"],"analyzedSha":"c3f553c740ebf3dff5e22dae24e9caaafafddd2d","analyzedAt":"2026-07-31T18:49:43.633Z","schemaVersion":2}