postman莫名奇妙报错,可能是注释引起的。postman 过滤请求体中的注释。

postman莫名奇妙报错,可能是注释引起的。postman 过滤请求体中的注释。

  • 1、问题描述
  • 2、问题分析
  • 3、解决方法

1、问题描述

postman http请求测试时,如果在请求体中添加了注释,那么这个注释会被带到服务端执行,导致服务端接口返回报错,无法正确执行,因为对于服务端来说,请求体中的注释是多余的,服务端不能正确解析请求体中的注释。

示例报错如下

postman莫名奇妙报错,可能是注释引起的。postman 过滤请求体中的注释。_第1张图片

2、问题分析

postman http请求测试时,如果在请求体中添加了注释,那么这个注释会被带到服务端执行,导致服务端接口返回报错,无法正确执行,因为对于服务端来说,请求体中的注释是多余的,服务端不能正确解析请求体中的注释。

3、解决方法

在postman 接口的Pre-request Script 添加 过滤请求体中的注释的脚本。

postman莫名奇妙报错,可能是注释引起的。postman 过滤请求体中的注释。_第2张图片

// 过滤请求体中的注释
if (pm?.request?.body?.mode === 'raw') {
    const rawData = pm.request.body.toString();
    const strippedData = rawData.replace(
        /\\"|"(?:\\"|[^"])*"|(\/\/.*|\/\*[\s\S]*?\*\/)/g,
        (m, g) => g ? "" : m
    );
    pm.request.body.raw = JSON.stringify(JSON.parse(strippedData));
}

加上 过滤请求体中的注释 脚本后,接口请求成功。

postman莫名奇妙报错,可能是注释引起的。postman 过滤请求体中的注释。_第3张图片

你可能感兴趣的:(默认,postman,postman,测试工具)