S
S
Sergey Malinin2020-01-14 09:45:54
JavaScript
Sergey Malinin, 2020-01-14 09:45:54

How to process a GET request for a non-existent element?

I made a small rest-api.
At the front, I need to implement the following logic:
- I form a GET request to get an element and a database by date.
- If the element is not found, at the front I collect the necessary data to create the element
- After the user pays for the order, I send a POST request to create the element.
I understand that with such logic it is possible to create a bunch of elements. This is not a problem, because a request to create an item is sent only after successful payment for the order.
I am making a request using Javascript.
Now I did this: I send a request, an error is returned to me: "Element not found, 404".
Further, I process this error, send a request to create an element.
Everything works correctly, the element is created, there are no problems. But at the same time, the same error is thrown into the developer console on the site: "request failed, element not found 404".
I'm self-taught, but I'm assuming that the console on the production site should never show errors. Is it so?
You can make improvements in the API itself, so that with such a request it gives a different response, which will be a signal for the next action, and not an error. But according to the REST-api concept, a GET request should generate an error.
What is the best way to improve this functionality?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Korotenko, 2020-01-14
@malininss

Do not make this request!
If no joke, then the status 404 is quite informative and normal, you can intercept and process it.
Also, use lists. For example, for a calendar
GET /api/getEvents/12.02.2020 -> returns a list of events 0..N and 200 status
GET /api/getEvents/12.02.2020/1 -> event number 1
POST /api/getEvents/12.02.2020 creation events
and so on

V
Vladimir Shapovalov, 2020-01-31
@Nexen2

Or make a json-api, where a record like [status: error, message: "not found"] will be returned.
I somehow made an enpoint /user/reset/{token} to reset passwords for users. Then there was also a reset of the email, for those who lost access to the soap or forgot the soap, a reset password and a reset email appeared. But I forgot to change the URL in the client. What happened next? That's right, the client thought that everything was fine, just bad clients palm off the wrong tokens day after day (which are not in the database, 404). Until a week later, with a merge of a completely different branch of the repository, they did not launch autotests that showed this bug.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question