Matteo Pagani
Matteo Pagani

Reputation: 545

Axios POST 404 not found

I want send email with form in react and to send the body to node i use axios.post this is my code:

handleSubmit = async e => {
e.preventDefault();

const { name, number, email, message } = this.state;

await axios.post("/send", { name, number, email, message });

console.log(this.state);
};

and the error messsage is:

POST http://localhost:3000/send 404 (Not Found)
createError.js:17 Uncaught (in promise) Error: Request failed with status code 404

i don't know why

Upvotes: 2

Views: 3892

Answers (1)

Amir Tahani
Amir Tahani

Reputation: 688

You have to set your backend server baseUrl you can do it like this

 await axios.post("baseUrl/send", { name, number, email, message });

or you can create instance of axios and set your base url and use your instance to send request

const instance = axios.create({
   baseURL: 'https://some-domain.com/api/',
   timeout: 1000,
   headers: {'X-Custom-Header': 'foobar'}
});

then use this instance to send requests

Upvotes: 2

Related Questions