When I submit a fetch in from my Next.js app in local development using netlify dev, the Netlify Function runs but the body in the event is always undefined
. I expect it to be { url: "websiteUrl" }
Here’s the fetch from my app:
const response = await fetch(
`/.netlify/functions/createDemoData-background`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ url: "websiteUrl" }),
}
);
Here’s the Netlify function, named: createDemoData-background.ts
import { HandlerEvent, HandlerContext } from "@netlify/functions";
const handler = async (event: HandlerEvent, context: HandlerContext) => {
console.log(event); // this is showing correctly
console.log("eventbody is", event.body); // this is undefined
return;
};
export { handler };
Here’s what the console outputs when the function fires:
Response with status 202 in 1 ms.
{
path: '/.netlify/functions/createDemoData-background',
httpMethod: 'POST',
queryStringParameters: {},
multiValueQueryStringParameters: {},
headers: {
'transfer-encoding': 'chunked',
host: 'localhost:8888',
'x-deno-pass': 'passthrough',
'x-nf-request-id': 'ed751452-2180-43a6-93eb-2299412f7a84',
'x-nf-geo': '{"city":"Somerville","country":{"code":"US","name":"United States"},"subdivision":{"code":"MA","name":"Massachusetts"}}',
'x-forwarded-for': '::1',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36',
'sec-fetch-site': 'same-origin',
'sec-fetch-mode': 'cors',
'sec-fetch-dest': 'empty',
'sec-ch-ua-platform': '"macOS"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua': '"Google Chrome";v="105", "Not)A;Brand";v="8", "Chromium";v="105"',
referer: 'http://localhost:8888/',
origin: 'http://localhost:8888',
'content-type': 'application/json',
connection: 'close',
'accept-language': 'en-US,en;q=0.9',
'accept-encoding': 'gzip, deflate, br',
accept: '*/*',
'client-ip': '::1'
},
multiValueHeaders: {
'transfer-encoding': [ 'chunked' ],
host: [ 'localhost:8888' ],
'x-deno-pass': [ 'passthrough' ],
'x-nf-request-id': [ 'ed751452-2180-43a6-93eb-2299412f7a84' ],
'x-nf-geo': [
'{"city":"Somerville","country":{"code":"US","name":"United States"},"subdivision":{"code":"MA","name":"Massachusetts"}}'
],
'x-forwarded-for': [ '::1' ],
'user-agent': [
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36'
],
'sec-fetch-site': [ 'same-origin' ],
'sec-fetch-mode': [ 'cors' ],
'sec-fetch-dest': [ 'empty' ],
'sec-ch-ua-platform': [ '"macOS"' ],
'sec-ch-ua-mobile': [ '?0' ],
'sec-ch-ua': [
'"Google Chrome";v="105", "Not)A;Brand";v="8", "Chromium";v="105"'
],
referer: [ 'http://localhost:8888/' ],
origin: [ 'http://localhost:8888' ],
'content-type': [ 'application/json' ],
connection: [ 'close' ],
'accept-language': [ 'en-US,en;q=0.9' ],
'accept-encoding': [ 'gzip, deflate, br' ],
accept: [ '*/*' ],
'client-ip': [ '::1' ]
},
body: undefined,
isBase64Encoded: false,
rawUrl: 'http://localhost:8888/.netlify/functions/createDemoData-background',
rawQuery: ''
}
eventbody is undefined