Issue
I am trying to call from an external API endpoint in NextJS, but I get the error:
{"level":50, Wed Jan 24 2024,"pid":4488,"hostname":"DESKTOP-S75IFN7","msg":"AxiosError: Request failed with status code 400\n at settle (webpack-internal:///(rsc)/./node_modules/axios/lib/core/settle.js:21:16)\n at IncomingMessage.handleStreamEnd (webpack-internal:///(rsc)/./node_modules/axios/lib/adapters/http.js:513:81)\n at IncomingMessage.emit (node:events:526:35)\n at endReadableNT (node:internal/streams/readable:1359:12)\n at process.processTicksAndRejections (node:internal/process/task_queues:82:21)"}
When I try to call from the API endpoint in PostMan, it works. The other API endpoints that I call from have the exact same API (/register and /login), but /favourites/add and /favourites/get-favourites don't work.
Here's the code in the NextJS API route:
import { NextRequest, NextResponse } from "next/server"
import { logger } from "@/utils/logger"
import axios from 'axios'
export const dynamic = 'force-dynamic'
const baseUrl = 'http://127.0.0.1:4000/api/favourites'
export const GET = async (req: NextRequest, route: { params: { userID: number }}) => {
console.log('Request Method: ', req.method)
console.log('Params: ', route.params.userID)
if (req.method === 'GET') {
try {
const userID = route.params.userID
const { data } = await axios.get(`${baseUrl}/get-favourites?user_id${userID}`)
if (data != null) {
return NextResponse.json({
result: data,
success: true
})
} else {
return new NextResponse('Failed to get favourites', {
status: 400
})
}
} catch (e) {
if (e instanceof Error) {
logger.error(`${e.stack}`)
return new NextResponse('Internal Server Error', {
status: 500
})
}
}
} else {
return new NextResponse('Not Implemented', {
status: 405
})
}
}
This is the error i get in the browser's console:
Solution
The error message indicates a status code 400. In your case, this is likely caused by incorrect data being sent to the API endpoint.
You forgot to add a =
in your URL after user_id
:
/get-favourites?user_id=${userID}`
Answered By - stonith404
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.