Question

I have what I think is a simple question about displaying a json file at a web address so that it can be recognized by Swagger UI.

The address http://petstore.swagger.wordnik.com/api/api-docs displays json data, and allows the file to be saved as a .json file. I have been unable to replicate this using html. This is what I tried:

<head>
<meta http-equiv="content-type" content="application/json; charset=UTF-8">
</head>
<body>
JSON DATA
</body>

The text gets displayed, however saving the page does not save it as a json file.

I should add that http://petstore.swagger.wordnik.com/api/api-docs is a directory, and http://petstore.swagger.wordnik.com/api/api-docs/pet is also json data.

Was it helpful?

Solution

Output this as your file:

JSON DATA

:)

You don't need HTML in there. Just your JSON data. You need to use PHP or an .htaccess file to provide the content-type header (must be done before outputting anything to the browser!). An HTML header I don't think will work as expected, as the content of the page is still HTML regardless of what you say it is. Regardless of content type, the actual content starts with HTML and so CTLR+S will save it with HTML content.

If using PHP, this file will work for you (save it as index.php and it will be the default when requesting a folder, as you requested in a comment on your question):

<?php header('Content-Type:application/json; charset=utf8'); ?>JSON DATA

You can put a linebreak after ?> and before JSON DATA if you prefer, PHP will ignore a single linebreak after a closing PHP tag.

This will make the browser detect the source is application/json, as opposed to interpreting it as an HTML file. However I suspect most browsers don't know what to do with a json file. If you want to force-download a file, (even suggesting a name like "my-amazing-file.json"), that can be done too:

How to force file download with PHP

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top