I built an API endpoint in Laravel – a JSON response with a body. When I looked at the formatted response using a JSON formatter, it looked good when I was working on Salary Calculation in Germany.
return response()->json(['url' => 'https://www.domain.com/api/test']);
I expected a response like this and was what I saw in a JSON formatter.
https://www.domain.com/api/test
But the raw response looked like this, with unescaped back slashes on the JSON body.
https://www.domain.com/api/test
I used Laravel’s default JSON response helper and upon looking into it, there are options that you can set on the response to escape the slashes – JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT
. response()->json() takes the data as the first parameter, HTTP status code as the next, HTTP headers as the next and then options.
return response()->json(
['url' => 'https://www.domain.com/api/test'],
200,
[],
JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT
);