M
M
Maxim Pritchin2019-10-07 20:24:05
Laravel
Maxim Pritchin, 2019-10-07 20:24:05

How to make Laravel API return only json?

The problem is: the input can be form-data or a json table, the output must be a json response.
How to do it competently without crutches in the API Documentation There is no such solution

If a form without headings comes to the input, then the validator redirects me to the index page. If the header is Accept application/json, then the response is returned in json, but it ignores the data that came from the form

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
JhaoDa, 2019-10-07
@pritchin_maxim1

First, if "the input can be form-data or json table", then this is a fig API. In the non-crap API, everything is the same, square and beautiful, like in the army. And there are no tables in json.
Secondly, we open App\Exceptions\Handler, go to the parent class, find the method convertValidationExceptionToResponse, override it in the child class and change the behavior to the desired one.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question