0
votes

I'm facing an error POST 404 (Not Found) as shown in another question that I posted.

Why does the axios.post return POST 404 (Not Found) error even when axios.get does return a valid value?

I was told that the reason why the axios post is not working even though axios get is working for the same address is that I need an endpoint for the POST method, which I haven't set.

I looked through several different pages, but I realized that defining an endpoint for POST method is not necessarily discussed, at least not explicitly using the word "endpoint".

http://blog.vuejoy.com/4-axios/

https://github.com/axios/axios

http://zetcode.com/javascript/axios/

Is it really always necessary to have an endpoint when you try to call a POST method?

If it is, where should I define the endpoint? Within the same vue file where I make the POST call, or should I create another file and define the endpoint there and import it to my main App.vue file?

If not, why does the axios.post throw an error 404 Not Found?

2
In that question I didn't even know about setting an endpoint, whereas in this question my purpose is to ask about whether it is always required to define an endpoint, so they are 2 different questions. Could you drop the flag of duplicate, please? - Sean2014

2 Answers

1
votes

Is it really always necessary to have an endpoint when you try to call a POST method?

Endpoint is just jargon for "end of a communication channel". In this context, it means "the function that is called on the server when a URL is called with a particular HTTP method".

So yes, you need something that will respond to the HTTP request with the data you want.

NB: This isn't specific to POST requests.

If it is, where should I define the endpoint?

In the server-side code.

Within the same vue file where I make the POST call, or should I create another file and define the endpoint there and import it to my main App.vue file?

No. You need to write code so the server responds to the request. You can't make that happen using client-side code.

0
votes

In the question you are linking to, you are doing

GET localhost:4000/somefile **.json** (a **file**)

I assume it's because GET called on a .json file will be read by the server as "get the contents of somefile.json" which is okay, and it will return it.

However for POST, the server has no predefined response - you can't POST to a file. You can post to an endpoint. You need to create for example a localhost:4000/addKweet endpoint that will take your kweet and append your kweet on the server side with some type of file write operation.