Getting 405 & "Error calling backend API: Unexpected end of JSON input" error on Vercel deployment
Unanswered
Transvaal lion posted this in #help-forum
Transvaal lionOP
I created a next.js (taiwind css) web app. It uses chatgpt 3.5 turbo api in the backend (integrted). The app runs perfectly on the localhost server, but when i deployed it via Vercel, it gave me "Error calling backend API: Unexpected end of JSON input" error, 405 error whenever I enter an input text and send it to the API. I am not sure what the issue is here, added api key to environmetl variable section in Vercel, tried changing the endpoint etc. but doesnt work. What could be causing this. Also, sometime later aafter the request (and error), I get this message as well: Uncaught (in promise) Error: A listener indicated an asynchronous response by returning true, but the message channel closed before a response was received". Please help me out, thank you.
86 Replies
Also are your .env variables setup correctly for your API KEY on vercel
Transvaal lionOP
didnt import any .env files, only entered API_KEY title and its value.
Forest bachac
alright can you send code so i can see what's happening?
Transvaal lionOP
let p = async()=>{
try {
let t = e.split(",").map(e=>e.trim()).join(" ");
h(!0);
let r = await fetch("https://reciperoulette-mertincesu.vercel.app/api/generate-text", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
inputText: t
})
})
, n = await r.json();
if (console.log("API Response:", n),
h(!1),
n.output) {
let e = n.output;
console.log("Generated Text:", e),
i(e),
m.push("/output?text=".concat(encodeURIComponent(e)))
} else
console.error("Invalid response structure received from the API"),
c("Invalid input: Please enter valid ingredients in the form X, X, X,...")
} catch (e) {
console.error("Error calling backend API:", e.message),
h(!1)
}
} this is the part that the error occurs (this is from browser console). the bolt line specifically
try {
let t = e.split(",").map(e=>e.trim()).join(" ");
h(!0);
let r = await fetch("https://reciperoulette-mertincesu.vercel.app/api/generate-text", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
inputText: t
})
})
, n = await r.json();
if (console.log("API Response:", n),
h(!1),
n.output) {
let e = n.output;
console.log("Generated Text:", e),
i(e),
m.push("/output?text=".concat(encodeURIComponent(e)))
} else
console.error("Invalid response structure received from the API"),
c("Invalid input: Please enter valid ingredients in the form X, X, X,...")
} catch (e) {
console.error("Error calling backend API:", e.message),
h(!1)
}
} this is the part that the error occurs (this is from browser console). the bolt line specifically
Forest bachac
is your frontend also on the same domain
assuming that, just use /api/generate-text/
Transvaal lionOP
thats what i run on localhost and it works, but i tried it on vercel build, same error ocurs
Forest bachac
try removing json.stringify
Transvaal lionOP
it was initialy like that, I changed it to this later on
Forest bachac
hm
can you send your backend
Transvaal lionOP
since it works as expected on localhost, i dont think there is a code issue but i m new to this so not sure
Forest bachac
i doubt its a vercel issue so can you just send it rq
wait does t exist
try changing t to just a string to see if it works
Transvaal lionOP
changing what?
Forest bachac
body: JSON.stringify({
inputText: t
})just change it to inputText: "something"
to see if t is the issue
Transvaal lionOP
sure one sec
same error
Forest bachac
if that doesnt work send backend code
@Forest bachac if that doesnt work send backend code
Forest bachac
^
Transvaal lionOP
can you see the code?
Forest bachac
are you using app router or pages router
Transvaal lionOP
this is the code where i integrated the api
pages
Forest bachac
the error is occuring on the backend?
Transvaal lionOP
nothing is said on the console or vercel logs, the only part that i see the error is the first code snippet I sent on the browser
Forest bachac
you sure nothing in the vercel console?
const API_KEY = 'XXXXX';you need that to be your api key in the process.env
process.env.API_KEY probably
Transvaal lionOP
so what should I do exactly? I didnt want to share the exact key here, but normally its hardcoded there
Forest bachac
is it hardcoded?
Transvaal lionOP
yes, normay in the place of XXXXX i have my full api key
Forest bachac
alr
that isn't best practice but isnt causing the issue then
Transvaal lionOP
is there a seperate conole for vercel, other than the one i build my app and run 'vercel' for deployment?
Forest bachac
yeah on vercel.com
https://reciperoulette-mertincesu.vercel.app/api/generate-text is this the correct link?
i tried sending a req and it said method not allowed
Transvaal lionOP
that should be the link for the generate-text serverless function
Forest bachac
what do you run to start the server btw
just vercel --prod?
Transvaal lionOP
just vercel
Forest bachac
it doesnt send you a link?
thats differe
Transvaal lionOP
it gives me 2 links, one for inspect and one for preview
Forest bachac
whats the preview link
Transvaal lionOP
Forest bachac
can you screenshot the console when you run vercel
Transvaal lionOP
Forest bachac
[Error] Failed to load resource: the server responded with a status of 405 () (generate-text, line 0)
[Error] Error calling backend API: – "The string did not match the expected pattern."
(anonymous function) (index-24d4a5635d91f649.js:1:8723)
asyncFunctionResume
(anonymous function)
promiseReactionJobWithoutPromise
promiseReactionJobyour backend isn't accepting post requests rn
alright.
what file is your backend code in
Transvaal lionOP
3 have 3 main files, index.js (main page that accepts user input etc.) output.js (to dispy the api response) and generate-text.js for api integration
all are under pages
Forest bachac
can you send a ss of your file directory
like showing where each file is
the generate-text.js should be in the pages/api folder
Transvaal lionOP
yes it is
Forest bachac
export default async function handler(req, res) {
if (req.method === 'POST') {
this is the file
if (req.method === 'POST') {
this is the file
?
Transvaal lionOP
yes in generate-text.js file
Forest bachac
thats really weird its not registering /api/generate-text for post requests
Transvaal lionOP
yeah can't seem to figure out why
Forest bachac
it works completely fine locally?
did you run vercel again when you made edits
does it work if you do vercel dev
Transvaal lionOP
yes works perfectly locally
let me try vercel dev
yes when i change the endpoint (fetch) to /api/generate-text it works perfectly
on dev
Forest bachac
?
and if you run vercel again
export const dynamic = 'force-dynamic' try adding this to page.jsTransvaal lionOP
where shoud i add it
Forest bachac
just like in the file like above your export function for the main code
Transvaal lionOP
added, same error
Forest bachac
tbh idrk then
Transvaal lionOP
all good, thank you for your help.
hope someone else can help me fix it...
Vaux's Swift
Hey, I had this same problem but it turns out I had missing env variables that the apis tried to use on vercel which I had set on local development.