Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

250
Views
code: 'ERR_HTTP_HEADERS_SENT' } error - uncaughtException: Error [ERR_STREAM_WRITE_AFTER_END]: write after end

Am trying to send a request to the server to save a post. Am using Nextjs and everytime i try to send it gives me this Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client.

Here is my code

Post.js
async function uploadPost(post) {

const response = await fetch('/api/savePost', {
  method: 'POST',
  body: post,
});

if(!response.ok) {
  throw new Error('Failed to post')
}

return await response.json();

}

savePost.js

export const config = {
      api: {
      bodyParser: false,
   },
}

export default const post = async (req, res) => {
  if (req.method !== 'POST') {
    return res.status(405).json({
     message: 'Method not allowed'
    })
  }

  // const postData = JSON.parse(req.body);
  const savedPost = await prisma.post.create({
   data: req.body
  });

  res.status(200).json({message: 'Post saved successfully'})
  res.json(savedPost)
 };

Am using Prisma for the db I get this error

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at new NodeError (node:internal/errors:371:5)
at ServerResponse.setHeader (node:_http_outgoing:576:11)
at NodeNextResponse.setHeader (C:\Users\user\Documents\My Received Files\js\twitter-clone\node_modules\next\dist\server\base-http\node.js:56:19)
at DevServer.renderError (C:\Users\user\Documents\My Received Files\js\twitter-clone\node_modules\next\dist\server\base-server.js:1134:17)
at DevServer.renderError (C:\Users\user\Documents\My Received Files\js\twitter-clone\node_modules\next\dist\server\next-server.js:493:22)
at DevServer.run (C:\Users\user\Documents\My Received Files\js\twitter-clone\node_modules\next\dist\server\dev\next-dev-server.js:452:35)
at async DevServer.handleRequest (C:\Users\user\Documents\My Received Files\js\twitter-clone\node_modules\next\dist\server\base-server.js:307:20) {
 code: 'ERR_HTTP_HEADERS_SENT'
}
error - uncaughtException: Error [ERR_STREAM_WRITE_AFTER_END]: write after end
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

  res.status(200).json({message: 'Post saved successfully'})  
  res.json(savedPost)

Here you send 2 messages back. Both with 200 code. This is what "write after send" means.

The first message set's the header and goes back.

Then after that you try to do it again, but the header is already sent.

// res.json() equals to res.status(200).json()

// You can merge res.status(200).json({message: 'Post saved successfully', savedPost: savedPost})

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!