JSON API

The baconipsum.com API is a REST interface that returns JSON. Pass in the following parameters using an HTTP GET and we’ll return a JSON string array of paragraphs.
It can also be accessed via https.

Parameters:

  • type: all-meat for meat only or meat-and-filler for meat mixed with miscellaneous ‘lorem ipsum’ filler.
  • paras: optional number of paragraphs, defaults to 5.
  • sentences: number of sentences (this overrides paragraphs)
  • start-with-lorem: optional pass 1 to start the first paragraph with ‘Bacon ipsum dolor sit amet’.

Examples:

Some sample jquery code!

$(document).ready(function() 
{
	$("#baconButton").click(function()
	{
		$.getJSON('http://baconipsum.com/api/?callback=?', 
			{ 'type':'meat-and-filler', 'start-with-lorem':'1', 'paras':'3' }, 
			function(baconGoodness)
		{
			if (baconGoodness && baconGoodness.length > 0)
			{
				$("#baconIpsumOutput").html('');
				for (var i = 0; i < baconGoodness.length; i++)
					$("#baconIpsumOutput").append('<p>' + baconGoodness[i] + '</p>');
				$("#baconIpsumOutput").show();
			}
		});
	});
});

Run the sample!

14 Responses to JSON API

  1. Vanessa says:

    Dudes. This is awesome. That is all!

  2. violacase says:

    Can’t stop laughing…
    Very nice!

  3. violacase says:

    To be honest: It’s TOO funny. I wasn’t able to produce a single line of serious code the last 2 hours.

  4. sameer says:

    beautiful and tasty!

  5. Jim Smart says:

    Great!

    Can you please make it so this can work with CORS? Currently the baconipsum API can’t be used by many Javascript frameworks, the browser denies access (because you don’t explicitly specify otherwise).

    To fix this, you need to make your server push out the following header with your JSON response:

    Access-Control-Allow-Origin: *

    More details here: http://www.html5rocks.com/en/tutorials/cors/#toc-adding-cors-support-to-the-server

    Thanks in advance!

  6. Jim Smart says:

    Hey — thanks for the awesome super quick fix-up! :)

  7. Surekha says:

    Excellent!
    Thanks.

  8. Daniel Furze says:

    Wicked cool, thanks! :D

  9. Pall Thayer says:

    This is awesome. However, the api returns text with two spaces after a period. This is wrong. There should only be a single space after a period. http://www.slate.com/articles/technology/technology/2011/01/space_invaders.html

  10. @GunGeekATX says:

    You can always branch and do a pull request if you’d like anything changed: https://github.com/petenelson/bacon-ipsum

  11. Marnix says:

    *OINK*

  12. Jay says:

    I’m getting CORS issues.
    XMLHttpRequest cannot load http://baconipsum.com/api/?type=meat-and-filler&start-with-lorem=1&paras=2. No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Origin ‘http://run.plnkr.co’ is therefore not allowed access.

    I tried to create a basic example in a plunker, but I get that error message. It worked once, then on the 2nd occurrence it failed. Any ideas?

  13. @GunGeekATX says:

    I double-checked the output and it is returning “Access-Control-Allow-Origin: *” in the header, not sure why you’d be getting that CORS error.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>