A public Doccy API

A simple public API for processing Doccy formatted text over HTTP.

I’ve just finished setting up a simple API for processing Doccy request, it accepts HTTP PUT requests containing the text you want to transform and returns HTML.

Don’t point your browsers to http://doccy.api.nbsp.io/1.0/ as it won’t accept GET requests, instead see the examples below.

HTTP responses

200 OK
Everything worked! The equivalent HTML for your Doccy has been returned.
405 Method Not Allowed
If you accidentally send a HTTP GET or POST request instead of a PUT request, this status code will be returned.
500 Internal Server Error
When an error occurs in parsing your request, you will get a 500 status response and a HTML body with a short description of the error.

Node.js example

var http = require('http');

// The text to send:
var text = '{em: Awesome!} It didn\\'t crash.';

// Request definition:
var options = {
	host:	'doccy.api.nbsp.io',
	path:	'/1.0/',
	method:	'PUT',
	headers: {
		'Content-Type':		'text/plain',
		'Content-Length':	text.length
	}
};

// Handle the http response:
var handler = function(response) {
	var html = '';

	// Accept incoming data:
	response.on('data', function(chunk) {
		html += chunk;
	});

	// All data received:
	response.on('end', function() {
		if (response.statusCode == 200) {
			console.log(html);
		}

		else {
			console.log('<h1>Uh oh!</h1>');
		}
	});
};

// Send the text to the API:
http.request(options, handler).end(text);

PHP example

<?php
	
	// The Doccy text to convert:
	$text = '{em: Awesome!} It didn\\'t crash.';

	// Place your text into memory so curl can use it:
	$temp = fopen('php://temp/maxmemory:256000', 'w');
	fwrite($temp, $text);
	fseek($temp, 0);

	// Create curl request:
	$ch = curl_init();
	curl_setopt_array($ch, array(
		CURLOPT_URL				=> 'http://doccy.api.nbsp.io/1.0/',
		CURLOPT_PUT				=> true,
		CURLOPT_INFILE			=> $temp,
		CURLOPT_INFILESIZE		=> strlen($text),
		CURLOPT_RETURNTRANSFER	=> true
	));

	// Execute request and get the status code:
	$html = curl_exec($ch);
	$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

	// Request was a success:
	if ($code == 200) {
		echo $html;
	}

	// Request failed:
	else {
		echo '<h1>Uh oh!</h1>';
	}

Share your thoughts...

Brendan wrote on :

Wouldn’t a 400 Bad Request be more appropriate response than 500?

Rowan Lewis wrote on :

That would be suitable for errors that are expected, but not for unexpected errors on the server.

At least that’s my take on it.