You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Karate调用本地Express接口返回426而非200的问题排查

问题排查与解决思路

核心问题拆解

426状态码代表「需要升级协议」,结合响应里的Server: Websocket++/0.70标识,说明你的Karate请求被误路由到了WebSocket服务的处理逻辑,而非Express的普通HTTP接口。Postman能正常请求,是因为它的请求头完全符合HTTP规范,不会触发WebSocket的升级检测。

具体排查与修复步骤

1. 调整Express中间件注册顺序

Express中间件按注册顺序执行,如果WebSocket相关中间件(比如express-ws)注册在/users路由之前,会优先拦截请求。

// 错误示例:WebSocket中间件先注册,拦截了HTTP请求
const expressWs = require('express-ws')(app);
app.ws('/users', (ws, req) => { /* WebSocket处理逻辑 */ });

// 正确顺序:先注册普通HTTP路由,再注册WebSocket路由
app.get('/users', (req, res) => {
  res.status(200).json({ users: [{ id: 1, name: 'Test' }] });
});
const expressWs = require('express-ws')(app);
app.ws('/ws-users', (ws, req) => { /* WebSocket处理逻辑 */ });

2. 清理Karate的请求头

Karate可能自动添加了触发WebSocket升级的请求头,显式清空并保留标准HTTP头:

Feature: Test /users API
  Scenario: Get all users
    Given url 'http://localhost:3000/users'
    And headers { Accept: 'application/json', Connection: 'close' }
    And header Upgrade = ''  # 强制清空Upgrade头
    When method get
    Then status 200
    And match response == { users: [{ id: 1, name: 'Test' }] }

3. 避免WebSocket路径冲突

如果必须让WebSocket和HTTP共用路径,在Express里添加请求类型判断:

app.use('/users', (req, res, next) => {
  // 仅当是WebSocket升级请求时,才交给WebSocket中间件
  if (req.headers.upgrade?.toLowerCase() === 'websocket') {
    return next();
  }
  // 否则走普通HTTP路由
  next('route');
});

// 普通HTTP路由
app.get('/users', (req, res) => {
  res.status(200).json({ users: [{ id: 1, name: 'Test' }] });
});

// WebSocket路由
app.ws('/users', (ws, req) => {
  ws.on('message', (msg) => { ws.send('Received: ' + msg); });
});

4. 检查Karate依赖配置

确认pom.xml里没有多余的WebSocket相关依赖,只保留核心Karate组件:

<dependency>
  <groupId>com.intuit.karate</groupId>
  <artifactId>karate-core</artifactId>
  <version>1.4.1</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>com.intuit.karate</groupId>
  <artifactId>karate-junit5</artifactId>
  <version>1.4.1</version>
  <scope>test</scope>
</dependency>

验证手段

  • 在Karate请求中添加print responseHeaders,查看完整请求头,确认是否有Upgrade: websocket这类触发升级的字段。
  • 用curl模拟Karate的异常请求,复现问题:
curl -v http://localhost:3000/users -H "Upgrade: websocket" -H "Connection: upgrade"

如果curl返回426,说明是请求头问题;如果返回200,说明Karate的默认配置存在异常。

内容的提问来源于stack exchange,提问作者TheWanderer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 22:37:09