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

Postman发送POST请求时出现JSON语法错误:减号后无数字

问题

API在网站应用中运行完全正常,但在Postman测试POST请求时触发错误:SyntaxError: No number after minus sign in JSON at position 1 (line 1 column 2)。已确认Postman请求体字段名称正确,但错误依旧;仅POST请求存在该问题,GET和DELETE请求在Postman中可正常工作。

网站添加商品到购物车的JSON响应示例:

{
id: 70,
productId: 21,
userId: 10,
size: 'Medium',
customerQuantity: 1
}

API代码如下:

import prisma from "@/app/prismadb";
import { NextResponse } from "next/server";

export async function POST(request: Request) {
    const body = await request.json();
    const { productId, userId, size, customerQuantity } = body;

    try {
        const existingCartItem = await prisma.basket.findFirst({
            where: {
                productId,
                userId,
                size: {
                    contains: size, 
                }
            },
        });

        if (existingCartItem) {
            await prisma.basket.delete({
                where: {
                    id: existingCartItem.id,
                },
            });
        }

        const product = await prisma.basket.create({
            data: {
                productId,
                userId,
                size,
                customerQuantity
            },
        });

        return NextResponse.json(product);
    } catch (error) {
        console.log("Error adding product to cart", error);
        return NextResponse.error();
    }
}

export async function DELETE(request: Request) {
    const body = await request.json();
    const { productId, userId } = body;

    try {
        const deleteItem = await prisma.basket.deleteMany({
            where: {
                productId: productId,
                userId: userId,
            },
        });

        return NextResponse.json(deleteItem);
    } catch (error) {
        console.log("Error deleting product", error);
        return NextResponse.error();
    }
}
分析与解决办法

核心问题定位

报错提示JSON解析失败,位置1的减号后无数字,说明Postman发送的请求体不符合标准JSON格式,或者请求头的Content-Type配置错误,导致后端request.json()无法正确解析。

具体解决步骤

  1. 检查并设置请求头Content-Type

    • 确保Postman的「Headers」标签中,Content-Type的值为application/json,否则后端无法识别请求体格式。
  2. 修正请求体为标准JSON格式

    • 标准JSON要求:
      • 所有键名必须用双引号包裹(不能用单引号或无引号)
      • 字符串值必须用双引号(不能用单引号)
      • 正确示例:
        {
          "productId": 21,
          "userId": 10,
          "size": "Medium",
          "customerQuantity": 1
        }
        
    • 你提供的网站响应是JS对象格式(非标准JSON),Postman发送时必须转为标准JSON。
  3. 确认Postman编辑器模式

    • 请求体选择「raw」模式,右侧下拉框必须选中「JSON」,而非「Text」或其他格式。
  4. 排查请求体冗余符号

    • 检查请求体开头是否误输入了减号-,这是报错提示的直接触发点。
    • 可通过浏览器开发者工具的Network标签,复制网站实际发送的请求体,直接粘贴到Postman中使用。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 22:40:55