Laravel API: "Force" to Return JSON (even without client headers)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys today I want to show you a trick with laral apis that I saw on Twitter by Newton job I just want to expand into a full example using that screenshot so first the problem when working with apis quite often people forget to use headers to return Json here's my example based on one of the older courses about API like booking com and I will link the full course in the description below if you want to check that out but basically there's an API call to search for apartments and properties and this is all fine if it works well by default it returns Json because it's supposed to in the code we have API resource with collection and by default larl transforms that into a Json fine but if we make any mistake in the code any error any bug for example let's comment out random line of get and then of course there will be an error and now if we send that request from Postman we get HTML as a result result so it's not a Json it's 500 internal server error and the API client whether it's JavaScript or mobile or whatever cannot really parse that because it's HTML there's no structure so to prevent that when using the API of course the solution is in headers you need to provide accept application Json and then if you send the error comes out with structured response in Json which can be parsed in the API client and a lot of times in my career I saw people asking me why my API returns HTML and at some point I even wrote the tutorial of how to fix HTML and not Json and since then I've been sending this list to everyone who asks that question but getting back to this trick on Twitter by Newton job we can force that header to be sent automatically even if the API client didn't actually send it so let's do exactly that step by step we need to create a middleware so PHP make middleware always accept Json then inside of that middleware we have request and middleware is not just for preventing something but also you can add something to that request and pass that to the next request another typical example would be setting something in the config or setting the main language or setting something in the session so in this case we have request headers set and then key value accept application Json like this so we have our middleware class now we need to assign it to API routes and in my case I don't have one route group for everything so we need to create that so one of the option is to create a route group here and assign that middleware here but also if we have all the apis powered by application Json another place to set that is in route service provider where we have API routes configured here so as you can see by default middleware is API and we can add another middleware here so API and then always accept Json class like this so now if we try it out by removing that checkbox and still sending the request it's still in Json because we're forcing it final thing there's another place where you can add that middleware in the same API so API is actually not one route but route Group which is defined in the kernel PHP of laravel app http kernel there's middleware groups and there's API Group so another place where we can Define it is here always accept Json class and let's test it out again whether it works still works so it's your personal preference on which level to add that middleware in the kernel in the route service provider or within specific routes here in the routes API what do you think about this suggestion maybe you have other ways to kind of force the application Json to be sent or maybe to catch that exception and provide the Json return anyway let's discuss in the comments below and see you guys in other videos
Info
Channel: Laravel Daily
Views: 6,919
Rating: undefined out of 5
Keywords:
Id: QhzrVlPm6wk
Channel Id: undefined
Length: 4min 9sec (249 seconds)
Published: Mon Oct 09 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.