Next.js 14.2.28 Unable to access request Body in api route #79764
Unanswered
sptungG
asked this question in
App Router
Replies: 1 comment
-
Hi, I've used I used this import { type NextRequest, NextResponse } from 'next/server';
import axios from "axios";
import queryString from "query-string";
const instance = axios.create({
baseURL: 'https://httpbin.dev', // Switched the baseURL here
paramsSerializer: {
serialize: (params) => queryString.stringify(params, { skipNull: true, skipEmptyString: true }),
},
maxRedirects: 0,
adapter: ["fetch", "xhr", "http"],
});
const interceptorReq = instance.interceptors.request.use(
async (config) => {
if (config.method && ["post", "put", "patch"].includes(config.method.toLowerCase())) {
console.log("Request Body:", config.data); // ---- still have body data
}
config.headers["Content-Type"] = "application/json";
return config;
},
(error) => Promise.reject(error),
);
instance.interceptors.response.use(
(response) => {
return response.data;
},
(error) => {
return Promise.reject(error);
},
);
const httpReq = instance;
const HttpRes = (data: any, status = 200) => NextResponse.json(data || {}, { status });
export async function POST(request: NextRequest) {
try {
const body = await request.json();
return HttpRes(await httpReq.post(`/post`, body)); // Switch the endpoint to `/post`
} catch (error) {
const mappedError = error instanceof Error ? error.message : 'Unexpected Exception'
return HttpRes(mappedError, mappedError.status);
}
} And this is what I get: ➜ curl localhost:1234/api -H 'Content-Type: application/json' -d '{"foo": "bar"}' -X POST
{
"args": {},
"headers": {
"Accept": [
"application/json, text/plain, */*"
],
"Accept-Encoding": [
"br, gzip, deflate"
],
"Accept-Language": [
"*"
],
"Content-Type": [
"application/json"
],
"Host": [
"httpbin.dev"
],
"Sec-Fetch-Mode": [
"cors"
],
"User-Agent": [
"node"
]
},
"origin": "10.0.11.236",
"url": "http://httpbin.dev/post",
"method": "POST",
"data": "{\"foo\":\"bar\"}",
"files": null,
"form": null,
"json": {
"foo": "bar"
}
} As you can see, the very last key Could it be something wrong in your BE? |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
I am using NextJs with app directory and I am trying to access request's body in one api route but it is giving empty object. Here's the code:
Below is the image of the request I am sending via postman
here my BE log when use post nextjs api route
When I tried accessing request body direct the api in api route there and in that routes they are working perfectly.
Creating client with payload: {'name': 'abcnanccccccc', 'email': '', 'phone_number': '', 'phone_country': 'US', 'business': 'e70ec71a-3baa-4345-b3b9-e23b07476688'}
Client created successfully: {'id': '271a8311-f652-427d-a1d8-39707fcc5e8e', 'name': 'abcnanccccccc', 'phone_number': '', 'phone_country': 'US', 'email': '', 'business': UUID('e70ec71a-3baa-4345-b3b9-e23b07476688')}
environment information
Beta Was this translation helpful? Give feedback.
All reactions