N
N
nhpmi2020-06-12 01:13:38
RESTful API
nhpmi, 2020-06-12 01:13:38

How to correctly send data to restful api?

I watch lessons on laravel rest api. In the controller, in the index and show methods, they simply return models, and in the store, update, delete methods - response()->json(data, status code). Why?
Why is index not using response()->json($data, 200)? If you write like this, will it be a mistake? Or is it just lessons about "mini-application on the knee" and in normal projects they already return response()->json($data, 200)? It seems to me safer on the front to first check the response to the 200 code, and then read the data. Or is it redundant?
And why is it needed in other methods?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
jazzus, 2020-06-12
@nhpmi

If LaravelResource is returned in the video in the index, then the response code is included in the bundle. It needs to be used when you need to return resources to the front in the form in which they are needed there. If resources are not needed (for example, after store), then Json Response is enough, as in your example.

S
segio_tt, 2020-06-12
@art055

In my opinion, the reason is very simple, the store update deleted methods first get something and only fulfill their mission, and if it’s about 200 ok, then when I pass something through the axios to the backend, for example, to the store or delete, it first checks Is there such a way then it does its job. If something is wrong, you can correct it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question