skip to Main Content

When I use my API on my website application it works perfectly fine, however when I try testing it in Postman I get SyntaxError: No number after minus sign in JSON at position 1 (line 1 column 2). I’ve put the data into the body in Postman and made sure that the names are correct but the error still persists. The weird thing is, I only get this with POST request. My GET and DELETE requests work fine from Postman. The JSON response after adding the product to the basket from the website is:

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

But when I use postman put the required data in body it gives the error in the title

The API code:

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();
    }
}



2

Answers


  1. Chosen as BEST ANSWER

    Made a silly mistake, the API was expecting JSON data in the request body and not form-data as I was doing in Postman. Changing it to raw instead of form-data and selecting JSON from dropdown menu fixed the problem.


  2. You don’t show what exactly you are putting in the postman body, but if you are putting in what you call the "JSON response", that will give you an error, since it is not JSON. It is a Javscript object, but not valid JSON.

    You would need to change your:

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

    to

    { "id": 70, "productId": 21, "userId": 10, "size": ‘Medium’, "customerQuantity": 1 }

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search