{"record":{"id":"72c510dd5622e696","repo":"YMFE/yapi","slug":"json","errorCode":null,"errorMessage":"返回数据格式不是 JSON","messagePattern":"返回数据格式不是 JSON","errorType":"http","errorClass":null,"httpStatus":402,"severity":"error","filePath":"server/controllers/project.js","lineNumber":1130,"sourceCode":"    projectList = commons.filterRes(projectList, projectRules);\n    groupList = commons.filterRes(groupList, groupRules);\n    interfaceList = commons.filterRes(interfaceList, interfaceRules);\n    let queryList = {\n      project: projectList,\n      group: groupList,\n      interface: interfaceList\n    };\n\n    return (ctx.body = yapi.commons.resReturn(queryList, 0, 'ok'));\n  }\n\n  // 输入 swagger url 的时候 node 端请求数据\n  async swaggerUrl(ctx) {\n    try {\n      const { url } = ctx.request.query;\n      const { data } = await axios.get(url);\n      if (data == null || typeof data !== 'object') {\n        throw new Error('返回数据格式不是 JSON');\n      }\n      ctx.body = yapi.commons.resReturn(data);\n    } catch (err) {\n      ctx.body = yapi.commons.resReturn(null, 402, String(err));\n    }\n  }\n}\n\nmodule.exports = projectController;\n","sourceCodeStart":1112,"sourceCodeEnd":1140,"githubUrl":"https://github.com/YMFE/yapi/blob/59bade3a8a43e7db077d38a4b0c7c584f30ddf8c/server/controllers/project.js#L1112-L1140","documentation":"projectController.swaggerUrl proxies a user-provided swagger URL server-side. If the response body is null or not an object (i.e. not JSON — an HTML page, string, etc.), it throws '返回数据格式不是 JSON'; the catch wraps it in a 402 response.","triggerScenarios":"User enters a swagger URL in the import UI whose response is not a JSON object — e.g. a swagger-ui HTML page, an XML/WSDL doc, or an empty response.","commonSituations":"Pasting the swagger UI page URL instead of the JSON spec URL; endpoint returns CSV/text; wrong endpoint behind a redirect; axios auto-parsing disabled so data is a string.","solutions":["Use the JSON spec URL (e.g. http://host/v2/api-docs or /swagger.json), not the swagger UI HTML page","curl the URL from the server and confirm the body is JSON","Ensure the endpoint does not return a login/HTML page due to auth redirects","Check that the response Content-Type is application/json"],"exampleFix":"// before\nconst { url } = ctx.request.query; // url = 'http://host/swagger-ui/index.html'\n// after\n// import with url = 'http://host/v2/api-docs' (returns JSON object)","handlingStrategy":"validation","validationCode":"async function isJsonSpecUrl(url){\n  try { const r = await fetch(url, { headers: { Accept: 'application/json' } }); const j = await r.json(); return j && typeof j === 'object'; } catch (e) { return false; }\n}","typeGuard":"function isJsonObject(v){ return v != null && typeof v === 'object' && !Array.isArray(v); }","tryCatchPattern":"try {\n  const { data } = await axios.get('/api/project/swagger_url?url=' + encodeURIComponent(url));\n  if (data.errcode) throw new Error(data.errmsg);\n} catch (e) {\n  console.error('Swagger URL must return a JSON object:', e.message);\n}","preventionTips":["Paste the spec JSON endpoint (/v2/api-docs, /swagger.json), never the HTML UI URL","Pre-verify the URL returns application/json","Beware endpoints that redirect to login HTML pages","Keep import URLs in project settings up to date after API gateway changes"],"tags":["validation","swagger","http","import"],"backgroundTag":"non-json-response","analyzedSha":"59bade3a8a43e7db077d38a4b0c7c584f30ddf8c","analyzedAt":"2026-08-29T08:45:22.203Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}