Laravel Send URL with JSON

- 1 answer

Ad

i send url with json with this code

return response()->json([
    'code' => '200',
    'message' => 'success',
    "resource" => url()."/packages/calibre/images/products/".$artwork->image
]);

but when i try to test the response, it's show the link

http:\/\/localhost:8000\/packages\/calibre\/ etc..

how to fix it? it's have double slash and backslash (/)

Ad

Answer

Ad

This works fine,

$artwork->imag = 'img.jpg';

return response()->json([
            'code' => '200',
            'message' => 'success',
            "resource" => url("/packages/calibre/images/products/{$artwork->imag}")
        ]);

Response in Postman:

{
  "code": "200",
  "message": "success",
  "resource": "http://laravel.dev/packages/calibre/images/products/img.jpg"
}

Response in Web browser:

{"code":"200","message":"success","resource":"http:\/\/laravel.dev\/packages\/calibre\/images\/products"}

If you wish to remove slashes from the encoded URL do this:

  return response(json_encode([
       'code' => '200',
       'message' => 'success',
       "resource" => url('/packages/calibre/images/products/')
   ], JSON_UNESCAPED_SLASHES))->header('Content-Type', "application/json");

Response in web browser

{"code":"200","message":"success","resource":"http://verimarked.dev/packages/calibre/images/products"}
Ad
source: stackoverflow.com
Ad