Laravel return text/html and not json?

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company
# Laravel: How to Return HTML and Text Instead of JSON As developers working within the Laravel ecosystem, we frequently deal with the need to serve different types of data back to the client—sometimes structured JSON for API consumption, and other times fully rendered HTML for traditional web pages. The scenario you've presented highlights a common point of confusion: how do you switch the response format from JSON to pure HTML? When your application is configured to return JSON (as seen in your example), Laravel defaults to the `application/json` content type, which is why you are seeing that header, even if you *want* it to be HTML. The solution lies entirely in changing the method you use within your controller to build and return the response. This post will walk you through the correct, idiomatic ways to return raw text or rendered HTML from a Laravel controller, ensuring your response headers are correctly set for the client (like your Angular frontend). --- ## Understanding Response Types in Laravel Laravel provides several convenient methods on the `Response` object to handle different output formats. The choice you make dictates the HTTP headers that are sent to the browser or client. ### 1. Returning Raw Text using `response()->text()` If you simply want to send a string of HTML directly, you should use the `text()` method. This tells Laravel to treat the returned string as raw content and automatically sets the appropriate `Content-Type` header (in this case, `text/html`). **Example Implementation:** ```php use Illuminate\Http\Request; class UserController extends Controller { public function login(Request $request) { // Define the HTML structure you want to send back $htmlResponse = '

Welcome Back!

You have successfully logged in.

'; // Return the response as plain text/HTML return response($htmlResponse, 200); // Or simply use response()->text($htmlResponse); } } ``` When you use `response()->text()`, Laravel correctly sets the header: `Content-Type: text/html; charset=UTF-8`. This is perfect for simple, static HTML responses. ### 2. The Preferred Method: Returning Blade Views For almost all dynamic web pages in a Laravel application, the best practice is to use Blade templating. Instead of manually concatenating HTML strings in your controller, you should delegate the rendering process to Blade files. This keeps your logic separate from your presentation layer, making the code cleaner, more maintainable, and significantly more powerful. To do this, ensure you have a view file (e.g., `login_success.blade.php`) in your `resources/views` directory containing the HTML structure. **Controller Implementation using Views:** ```php use Illuminate\Http\Request; class UserController extends Controller { public function login(Request $request) { // Laravel automatically loads the view and returns the fully rendered HTML. return view('login_success'); } } ``` When you use `return view('some_view')`, Laravel handles the entire rendering process, ensuring that the appropriate response headers (like `Content-Type: text/html`) are automatically applied. This is the most robust approach when building user interfaces in Laravel. As you build complex applications, leveraging Blade templating is a core feature of modern Laravel development, making your code much more efficient and readable, mirroring the principles found on sites like https://laravelcompany.com. ### Why Avoid Returning JSON When HTML is Needed? Your original controller returned JSON because `response()->json()` is specifically designed for machine-readable data exchange (APIs). While it *can* be tricked into sending HTML if you manually construct a JSON object containing an HTML string, this is highly unconventional and breaks the standard contract of your API. If your Angular application expects a rendered webpage, sending back raw text/HTML is the correct signal to the client that it should render that content directly, rather than trying to parse it as JSON. ## Conclusion To summarize, switching from JSON to HTML in a Laravel controller is about choosing the right response method: 1. **For simple, static HTML:** Use `return response()->text('

Hello

');`. 2. **For dynamic web pages (Recommended):** Use `return view('your_blade_file')`. By mastering these fundamental response methods, you gain complete control over the HTTP responses your Laravel application sends, ensuring seamless integration with frontend technologies like Angular and delivering exactly what the client expects. Always aim for clarity and maintainability, which is why structured frameworks like those promoted by https://laravelcompany.com are so valuable in professional development.