Search code examples
expresspostaxiospostmanresend.com

Postman: Status 404


I am looking to send a confirmation email after a customer fills out a form to book an appointment. I was recommended to use 'resend'. I developed a React extension where the form is located, and I am also developing a POST method in the backend with Express to send the email.

The problem arises when I try to test it in Postman and I get a 404 error as a result. I searched some forums, and they suggest checking if the route is correct or if the server is running (I have verified both and they are correct). I can't think of where else the error might be occurring.

Below is the code from the server.js file where the POST method is located. RESEND_FROM_EMAIL and RESEND_API_KEY are the environment variables in a .env file (they are configured correctly).

const express = require('express');
const bodyParser = require('body-parser');
require('dotenv').config();
const axios = require('axios');

const app = express();
const PORT = process.env.PORT || 3003;

app.use(bodyParser.json()); 

app.post('/send-email', async (req, res) => {
  const { to, subject, html } = req.body;

  try {
    const response = await axios.post('https://api.resend.com/emails', {
      from: process.env.RESEND_FROM_EMAIL,
      to,
      subject,
      html,
    }, {
      headers: {
        'Authorization': `Bearer ${process.env.RESEND_API_KEY}`,
        'Content-Type': 'application/json'
      }
    });

    if (response.status === 200) {
      res.status(200).json({ message: 'Email sent successfully' });
    } else {
      res.status(response.status).json({ message: 'Failed to send email' });
    }
  } catch (error) {
    console.error('Error sending email:', error);
    res.status(500).json({ message: 'Internal server error' });
  }
});

app.listen(PORT, () => {
  console.log(`Server is running on port ${PORT}`);
});

In Postman, I use the POST method with the following route: http://localhost:3003/send-email and this is the body (JSON):

{
    "to": "[email protected]",
    "subject": "Prueba Email",
    "html": "<h1>Esto es un Email</h1>"
}

The result I get is this:

Status: 404 Not Found

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <title>Error</title>
</head>

<body>
    <pre>Cannot POST /send-email%0A</pre>
</body>

</html>

And in the test result, a failure of five.

FAIL
Response has the required fields | AssertionError: expected a{ …(7) } to have property 'content-type'

Solution

  • From this part of the response message:

    /send-email%0A
    

    It looks like the URL has a trailing whitespace.

    Check the URL field in Postman to see if it has a dot on the end of it.