YMFE/yapi · error

http status "${response.status}"获取数据失败,请确认 swaggerUrl 是否正确

Error message

http status "${response.status}"获取数据失败,请确认 swaggerUrl 是否正确

What it means

getSwaggerContent fetches a swagger JSON document via axios. If the HTTP status is greater than 400, it throws this error; the message embeds the status so users can tell the swaggerUrl is unreachable or wrong.

Source

Thrown at exts/yapi-plugin-swagger-auto-sync/interfaceSyncUtils.js:210

     * @param {*} syncMode 合并模式
     */
    getSyncModeName(syncMode) {
        if (syncMode == 'good') {
            return '智能合并';
        } else if (syncMode == 'normal') {
            return '普通模式';
        } else if (syncMode == 'merge') {
            return '完全覆盖';
        }
        return '';
    }

    async getSwaggerContent(swaggerUrl) {
        const axios = require('axios')
        try {
            let response = await axios.get(swaggerUrl);
            if (response.status > 400) {
                throw new Error(`http status "${response.status}"` + '获取数据失败,请确认 swaggerUrl 是否正确')
            }
            return response.data;
        } catch (e) {
            let response = e.response || {status: e.message || 'error'};
            throw new Error(`http status "${response.status}"` + '获取数据失败,请确认 swaggerUrl 是否正确')
        }
    }

}

module.exports = syncUtils;

View on GitHub (pinned to 59bade3a8a)

Solutions

  1. Open the swaggerUrl in a browser/curl from the server host and confirm it returns JSON with HTTP 200
  2. Fix the URL path — use the raw spec endpoint (e.g. /v2/swagger.json), not the swagger UI page
  3. Add required auth headers or make the spec endpoint publicly reachable for the YApi server
  4. Check network/proxy settings on the YApi server

Example fix

// before
syncUrl: 'http://host/swagger-ui.html'
// after
syncUrl: 'http://host/v2/api-docs'
Defensive patterns

Strategy: validation

Validate before calling

async function assertSwaggerUrlOk(url){
  const res = await fetch(url, { method: 'HEAD' }).catch(() => null);
  if (!res || res.status > 400) throw new Error('swaggerUrl unreachable or status ' + (res && res.status));
}

Try / catch

try {
  await syncInterface(swaggerUrl);
} catch (e) {
  if (/swaggerUrl/.test(e.message)) notifyAdmin('Fix swagger URL: ' + swaggerUrl);
  else throw e;
}

Prevention

When it happens

Trigger: syncInterface calls getSwaggerContent with a swaggerUrl that responds with status > 400 (e.g. 404 wrong path, 401 auth required, 500 server crash).

Common situations: Swagger URL pointing to a UI page instead of the JSON spec; missing auth token on the swagger endpoint; firewall/proxy blocking the server-side request; typos in host/port.

Understand the failure class

Background: 'Something went wrong' / 'Request failed (500)' / 'HTTP error! status: 404' — what failed HTTP requests actually mean and how to find the real cause — this error's family across 28 libraries.

Related errors


AI-assisted analysis of YMFE/yapi@59bade3a8a (2026-08-29). Data as JSON: /api/errors/843a9348bac2dc7c. Report an issue: GitHub.